|
What are the benefits and drawbacks of using dynamic TSQL? [In an effort to help seeding this site].
(comments are locked)
|
|
Erland Sommarskog's article, The Curse and Blessings of Dynamic SQL, is often cited as the canonical reference. That is the best reference I have found. To summarize it, there are some things which are effectively impossible to do without some form of dynamic sql (running a given query against an arbitrary table for instance). But when you have a choice between using dynamic SQL or not, then not using it is almost always the correct choice.
Oct 20 '09 at 12:18 PM
TimothyAWiseman
(comments are locked)
|
|
It depends on how you implement dynamic sql what the different benefits and drawbacks are. Do you mean adhoc SQL created on the client or dynamic SQL created in a stored procedure based on parameters passed in? Are you going to use Exec (@sql) or sp_executesql to execute the dynamic SQL? Some benefits may be:
Some drawbacks may be:
Erlaand does a good job in the article referenced and is usually what I would reference.
(comments are locked)
|
|
I think that Dynamic SQL is much maligned. It has a small place in many systems, and shouldn't be a first choice to solve a problem, but I prefer to choose generating dynamic SQL within the database (perhaps in a stored procedure) than allowing it to be used from outside the database. Its use should always be justified. Because every problem falls into different categories, I would say that the places I have used and justified dynamic SQL are: Dynamic PIVOTs - the PIVOT construct does not allow for a dynamic number of columns to be generated. In this case, I often generate the PIVOT from the data or metadata in order for the code to require no maintenance. Table comparison - I have a table/view comparison tool which uses the metadata about two tables/views and creates and executes SQL dynamically to analyze the differences between the tables, including options for what to consider to be keys and thresholds for data differences allowed in numeric columns. When looping through a variable set of tables, perhaps to append them all to a single table, it is not possible to do this in straight SQL, and generating such dynamic SQL outside the database is overkill, and actually riskier. A technique I tend to use a lot are code templates:
This allows the SQL to be more readable and maintainable in one place instead of throughout many concatenations. In all cases, dynamic SQL generated from metadata can be made extremely safe from SQL injection - typically they are only vulnerable if a column name is pathological.
(comments are locked)
|
|
Dynamic SQL is a blessing and curse. If a limited set of options are available, and inputted values are used as variables where possible, or (where variables won't apply) first checked for validity against system views (such as sys.objects or sys.columns), then it can be quite safe. If the variety of queries that can be run is large, then the query hint OPTION (RECOMPILE) should be considered, as there is no gain from having the query plan stored in the cache.
(comments are locked)
|

