views:

226

answers:

9

a) What do we consider as a dynamic sql statement?
Any sql statement that dynamically adds a clause(s) or even just a part of a clause to a SQL string?

b)Aren’t then parameterized strings that use placeholders for dynamically supplied values also considered dynamic sql statements?

thanx

+6  A: 

A dynamic SQL statement is a statement that is built at execution time. The emphasis lies on statement. So, it isn't dynamic SQL if you just supply a value at execution time.

Maximilian Mayerl
By this defintion, b) would not be considered Dynamic SQL, but since it speaks about a _string_ where a placeholder is substituted by a value, chances are a novel query/string will be passed to SQL which will make it dynamic SQL (in the SQL-centric definition thereof)
mjv
+3  A: 

Certainly anything involving EXEC (@sql) or EXEC sp_ExecuteSQL @sql, ... (i.e. dynamic at the database itself) would qualify, but I guess you could argue that any SQL generated at runtime (rather than fixed at build / install) would qualify.

So yes, you could argue that a runtime-generated, yet correctly parameterized query is "dynamic" (for example, LINQ-to-SQL generated queries), but to be honest as long as it doesn't expose you to injection attacks I don't care about the name ;-p

Marc Gravell
+1  A: 

A. Anything that will cause the DB server to evaluate strings as SQL.

B. No, as they still go through the DB driver/provider and get cleaned up.

Oded
+3  A: 

Dynamic SQL statements generally refers those that are constructed using string concatenation.

"SELECT name FROM names WHERE id=" + this.id;
"SELECT name FROM names WHERE id=" + this.id + " AND age=" this.age;

Parameterized queries are also dynamic but not in terms of construct. You can only change parameters but you can't change the structure of the statement i.e add WHERE clauses.

Parameterized queries are often at the database level so the database can cache the execution plan of the query and use it over and over. Not quite possible in the first case since a simple change in the text or the order of the where clauses can cause the database to not recognize the previously cached execution plan and start over.

The first construct is also vulnerable to SQL injection since it is hard to validate input values for attempts to inject rogue SQL.

Chetan Sastry
thank you all for your help
carewithl
+1  A: 

For point b) You already know the statement and you pass in known parameters (hopefully type safe and not string literals).

RandomNoob
if they are parameters (and treated as such), it doesn't really matter what the contents are...
Marc Gravell
Only if you are using SqlParamaters etc, consider: String sql = "Select * From myTable where Field={0}"If its a string literal I can certainly inject, thats all I'm sayin, so it does matter - thats the point I was trying to make, I have no idea how he is constructing his statement.
RandomNoob
+2  A: 

Dynamic sql is basically just any sql that is not fully constructed until runtime. Its generated on-the-fly by concatenating runtime values into a statement. Could be any part of an sql statement

AdaTheDev
+1  A: 

I see where you're going with this, but one somewhat objective criteria which defines a particular situation as Dynamic SQL vs. say a prepared statement is...

...the fact that dynamic statements cause the SQL server to fully evaluate the query, to define a query plan etc.

With prepared statements, SQL can (and does unless explicitly asked) cache the query plan (and in some cases, even gather statistics about the returns etc.).
[Edit: effectively, even dynamic SQL statements are cached, but such cached plans have a much smaller chance of being reused because the exact same query would need to be received anew for this to happen (unlike with parametrized queries where the plan is reused even with distinct parameter values and of course, unless "WITH RECOMPILE")]

So in the cases from the question, both a) and b) would be considered dynamic SQL, because, in the case of b), the substitution takes place outside of SQL. This is not a prepared statement. SQL will see it as a totally novel statement, as it doesn't know that you are merely changing the search values.

Now... beyond the SQL-centric definition of dynamic SQL, it may be useful to distinguish between various forms of dynamic SQL, such as say your a) and b) cases.
Dynamic SQL has had a bad rep for some time, some of it related to SQL injection awareness. I've seen applications where dynamic SQL is produced, and executed, within a Stored Procedure, and because it is technically "SQL-side" some people tended to accept it (even though SQL-injecting in particular may not have been addressed)...
The point I'm trying to make here is that building a query, dynamically, from various contextual elements is often needed, and one may be better advised to implement this in "application" layers (well, call it application-side layers, for indeed, it can and should be separate from application per-se), where the programming language and associated data structures are typically easier and more expressive than say T-SQL and such. So jamming this into SQL for the sake of calling it "Data-side" isn't a good thing, in my opinion.

mjv
Maybe Ive misunderstood what you're saying but, dynamic statements can be cached too. Theres just generally lower value in them as the plan will only be reused if the same statement is generated and executed
AdaTheDev
@AdaTheDev, right! you are correct all stmts are cached, it's just that these from dynamic SQL have much smaller chances of being reused. I'll edit to make this more explicit.
mjv
+1  A: 

I consider a dynamic SQL statement to be one that accepts new values at runtime in order to return a different result. "New values", by my reckoning, can be a different ORDER BY, new WHERE criteria, different field selections, etc.

spork
+1  A: 

a) What do we consider as a dynamic sql statement? Any sql statement that dynamically adds a clause(s) or even just a part of a clause to a SQL string?

Both - any query altered/tailored prior to execution.

b)Aren’t then parameterized strings that use placeholders for dynamically supplied values also considered dynamic sql statements?

Parameterized queries, AKA using bind variables, are supplying different filter criteria to the query. You can use (my_variable IS NULL OR ...), but OR performance is generally terrible on any db & the approach destroys sargability.

Dynamic SQL generally deals with tailoring the query to include other logic, like JOINs that only need to be included if a specific parameter is set. However, there are limitations like the IN clause not supporting converting a comma delimited string as it's list of options - for this you would have to use dynamic SQL, or handle the comma delimited list in another fashion (CLR, pipelined into a temp table, etc).

OMG Ponies