If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Dynamic or Static SQL exact definition...

LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Registered User
Join Date: Mar 2004
Posts: 370
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
Reply With Quote
  #2 (permalink)  
Resident Curmudgeon
Join Date: Feb 2004
Location: In front of the computer
Posts: 14,815
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";
Reply With Quote
  #3 (permalink)  
Registered User
Join Date: Sep 2002
Location: Sydney, Australia
Posts: 255
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.
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

Last edited by DerekA; 02-07-05 at 09:09.
Reply With Quote
  #4 (permalink)  
SQL Consultant
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 20,000
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

rudy.ca | @rudydotca
Buy my SitePoint book: Simply SQL
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On