Why won't Dynamic SQL Die?

Published Sat, Mar 20 2004 15:43 | William

On a regular basis someone posts a question on the NG's about how apostrophes are screwing up their code and what can be done about it.  So let me be blunt:  DYNAMIC SQL NEEDS TO DIE.  Period, end of story.  For a long time, I was rather passive about this subject, but I think that's no longer appropriate.  So, I'm going to use every venue I have at my disposal to beat this point into the ground.  Why?

  1. Dynamic SQL is inefficient.  Using Stored Procedures is obviously the 'best' solution, but I've heard a lot of folks tell me due to ridiculous corporate politics, they aren't allowed to use Stored Procs.  This is so absurd it's like something out of Dilbert, but regrettably it's true.  However, you can still construct Paramaterized queries without using procs.  This will allow for cached execution plans which is often going to afford rather profound performance differences, particularly in large multiuser environments. 
  2. Dyamic SQL is verbose and ugly.  I'll let an example speak for itself.  Compare these two statements which produce the same resultset.

          “SELECT * FROM SomeTable WHERE SomeColumn = '“ & SomeValue & “', AND SomeotherValue = '“ & SomeOtherValue “', AND SomeFinalVaue = '“ & SomeFinalValue & “'“

         VS.

         “SELECT * FROM SomeTable Where SomeColumn = @SomeValue and SomeOtherValue = @SomeOtherValue AND SomeFinalValue = @SomeFinalValue“

           cmd.Parameters.Add(“@SomeValue“, SqlDbType.Varchar, 50).Value = SomeValue

           cmd.Parameters.Add(“@SomeOtherValue“, SqlDbType.Varchar, 50).Value = SomeOtherValue

           cmd.Parameters.Add(“@SomeFinalValue“, SqlDbType.Varchar, 10).Value = SomeFinalValue

The parameterized version is much easier to construct (no string concatenation) and everything is clear and understandable.  Compare that with the slop above it and the results speak for themselves.

     3.  Dyamic SQL Is Error Prone.  There are many people with Irish surnames in this world like O'Brien for instance.  That will cause your Dynamic SQL Statement to blow up.  So, you can either use some String.Replace function to replace the single quotes or you can have it blow up on you.  But, let's say you have 100 queries in your app.  That's a whole lot of extra code but more importantly, you're probably going to forget to escape it at one point or another. And even if you are really anal and do this perfectly, another programmer not so familiar with the code will invariably forget it.  And the best part is that in most instances, the bug won't show itself in early testing.  It will probably come to your attention from a customer who can't do their job.  So you fix it, recompile, and redistribute because there's no other way to fix the problem.  This will not make you look like a competent professional

     4.  Dynamic SQL Is Dangerous.   Too many hackers have figured out this vulnerability and they may well try to hit your database.  Security by Hoping No One Pays attention to your app is a shameful policy and it could easily cost you your job, your company a lot of money and a lot more depending on what your app does.

Sure seems like a hell of a lot of downside just to keep a terrible habit.  I've got a lot of Articles on KnowDotNet about this subject, and I'll have a lot more soon.  Hopefully you can help spread the word and help send Dynamic SQL to an early grave!

Comments

# William said on March 23, 2004 10:10 AM:

I would be happy to contribute the excel code if you like.

Did you see my Word version?

I know I know I should know better than to use dynamic SQL but it was an internal application (within my network) and I didnt really feel that that was too much of a threat.

Crystal Reports - UGH I hate it.

It caused some very adverse effects on folks computers especially the Windows 2000 computers.

I don't like the limitations it puts on me. Sometimes I feel like I have to learn another language.

And to be honest, I love learning how to do what Crystal seems to make so easy. I don't want a tool that does everything for me. Call me crazy I guess....

By the way I am clicking on your site reguarly today.....:)

# William said on March 23, 2004 10:17 AM:

Hey while you are there in the next ADO.NET update it would be nice if they would make the table.select and table.compute methods more flexible. You can show them that thread and ask them why filters were being ignored....

Just a thought.

# William said on March 23, 2004 2:09 PM:

Hey Scorp:

I posted these a few days ago, but it's not in relation to the code you showed me ;-). Looks suspicious I know, but it's been a pet peeve of mine for a while.

I actually have the code and it's definitely on my priority list. I've also found that AVG on an INT column has all kinds of rounding issues and I've yet been able to use a cast/convert effectively (even though the documentation says you can). Even if there is a valid work around, I know this has given a lot of people a lot of grief and the documentation needs updated. I'll be in touch later this evening and let you know what I find out.

Thanks again for writing and if you think of anything else, let me know.

Cheers,

Bill

# William said on March 23, 2004 2:12 PM:

Scorp:

Have you had a chance to look at SQL Server 2000 Reporting Services? It's free, is extremely intuitive and allows you to flip from Design to Preview like Crystal Reports used to let you do. I got to play with it at DevDays last week and since then quite a bit. It's very very cool, and if you are doing a lot of reporting, it's well worth looking into.

When I get back Saturday, we'll talk some more about the article, but I think your code would be great and we can mix the Word stuff with it (and XML) and have ourselves a great little article.

Cheers,

Bill

# William said on March 23, 2004 5:23 PM:

Very cool plan. You ae so lucky I bet you are having a blast. I suspect a lot of people will turn on to this just because working with Excel and Word are so integral to our tasks in companies.

Reporting is my life such as it is. Is there a sample app in the 101 vb.net samples that demonstrate the SQL Server 2000 Reporting Services?

# William said on March 24, 2004 7:17 AM:

Thanks Scorp. Yeah, i could definitely get used to this sort of thing..too bad there aren't any really high paying jobs who's onily requirement is attending these things.

I'll have some more info posted later today.

Cheers,

Bill


W.G. Ryan MVP
www.knowdotnet.com

# TrackBack said on July 6, 2004 1:55 PM:

Dynamic Sql

# TrackBack said on July 24, 2004 5:00 PM:

Dynamic SQL

# William said on November 6, 2004 4:36 PM:

what if you're not sure how many parameters will be used? how would you use your method for that?

# William said on November 6, 2004 5:06 PM:

You can still use Paramaterized Queries and construct the dynamically. You need to know this in advance anyway when using a regular dynamic sql construct, so by using Paramaters instead, you mitigate most of the potential damage that can be done.

# William said on March 19, 2005 4:21 AM:

Here you go:
1. Paging
2. Dynamic Search Conditions
3. Dynamic Filters
4. Dynamic Reports
5. Dynamic databse structure
6. Correctly constructed and executed dynamic SQL is as efficient as a Stored Procedure

Of course, stored procedures and paramterized queries are the way to go for structured data access, however, dynamic SQL is (unfortunatly) the only way in some dynamic scenarios.

# William said on September 26, 2005 9:43 PM:

this post look convincing... but what if in the 'where' part needs to use the reserved work 'like'?

such as "where someValue like '%" & something & %'"

I won't be able to use the @SomeValue and set parameters already, right?

# William said on September 26, 2005 10:05 PM:

You're correct, for that instance, there's this http://support.microsoft.com/default.aspx?scid=kb;en-us;555167

# Bill's House O Insomnia said on January 15, 2006 9:50 AM:

One new object in the ADO.NET 2.0 Library worth taking a look at is the SqlConnectionStringBuilder . ...

Search

This Blog

Tags

Community

Archives

News

My other sites

Cool Stuff

Book Stuff

Security

ORM

Data Access

Funny Stuff

Compact Framework Stuff

Web Casts

My KnowledgeBase Articles

My MVP Profile

Design Patterns

Performance

Debugging

Remoting

My Fellow Authors

My Books

LINQ

Misc

Speech

Syndication

Email Notifications