Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2004

    Unanswered: Dynamic or Static SQL exact definition...

    Is ther any body who kindly explain me the exact meaning of Dynamic and Static SQL please? I tought know this but it seems I got it wrong!
    some EXAMPLES would be so appreciated
    -Best regards

  2. #2
    Join Date
    Feb 2004
    In front of the computer
    Provided Answers: 54
    The difference between dynamic and static SQL lies in how the SQL statement itself is generated. A statement that is always submitted the same way, is fully static. A statement that allows parameter substitution is considered static, even though there will be small changes in each usage (due to changes in the parameters). A statement that is composed "on the fly" is considered fully dynamic, meaning that the SQL engine will get wildly varying SQL statements each time the dynamic SQL is used.

    Some examples using Perl would be:
    $fooID = "'bar'"; # just a SQL constant
    $col_list = "*"; # could be any list of columns
    $table = "foo"; # table name
    $where = "where 0 = 1"; # returns no rows at all
    $static = "SELECT * FROM foo";
    $parm = "SELECT * FROM foo WHERE fooID = $fooID";
    $dynamic = "SELECT $col_list FROM $table $where";

  3. #3
    Join Date
    Sep 2002
    Sydney, Australia

    Worlds Apart

    My, how different the worlds that we live in are !

    I am sure that Pat's definition is correct in a context that is unknown to me, but in the Sybase world:
    "Dynamic SQL" is what happens when you send a batch of SQL to the server. The server parses it, builds a query plan (identifies and resolves referenced objects) for it, builds a query tree (identifies resources) for it, allocates resources for it, executes it, and sends back the result set.

    "Static SQL" is what happens when you put the [possibly same] SQL into a stored procedure and compile it (once). The server parses it, builds a query plan for it, builds a query tree for it (once). Then every time you execute the stored proc, it grabs the query plan (it may already be in memory for another user), allocates resources, executes it and returns the result set. Much faster because the required objects are known, parsing is not required, etc, and yes, changes to the objects referenced in the stored proc force it to be recompiled. Dynamic VALUES (known only at runtime) are passed as parameters to the static query, and this is quite ordinary. Dynamic SQL also has various limitations (eg. Exec(); no of verbs; error checking and passing back, to name just a few) which Static SQL does not have, but most important, it is hideous re transaction control.

    One characteristic of a 'poor' application is that is stores the SQL in the client-side program, and therefore is always running Dynamic SQL, with the above overheads. Further, such SQL may not always work (eg. due to values out-of-range, programming errors, etc). In this regard a 'good' application has all its SQL stored on the server (well once that particular release of the app has been developed, the SQL does not change), and the SQL always works. Server objects can be monitored and administered with a lot more ease than trying to catch a bunch of SQL executing on the fly.

    Some would say that SELECTs do not matter: to an extent that is true, but 'really good' apps (in this one regard) compile everything (why would you want to run at half the speed ?!?). (Of course, third party reporting tools cannot help but do Dynamic SELECTs).

    BTW (and I am not addressing what is clearly an example above) anyone actually implementing SELECT * FROM <table> or INSERT ... without a column list should change careers. Email me if you do not know why.
    Last edited by DerekA; 02-07-05 at 09:09.
    Derek Asirvadem
    Senior Sybase DBA/Information Architect derekATsoftwaregemsDOTcomDOTau
    Anything worth doing is worth doing Right The First Time
    Spend your money on standards-compliant development or spend 10 times more fixing it

  4. #4
    Join Date
    Apr 2002
    Toronto, Canada
    derek, nice exposition

    however, i take issue with a couple of your points

    you say "Much faster ..." and also "why would you want to run at half the speed ?!?)"

    please provide benchmark figures to back up these egregiously inflated claims

    if i've told you once, i've told you a million times, don't exaggerate

    email me if you don't know why this is unprofessional | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts