Home > Microsoft .Net Development Tips > Microsoft SQL Server > Choosing between stored procedures and dynamic SQL
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

MICROSOFT SQL SERVER

Choosing between stored procedures and dynamic SQL


Serdar Yegulalp, Contributor
09.07.2006
Rating: --- (out of 5)


.NET Essentials Channel
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


This tip originally appeared here on SearchSQLServer.com.

In the six years I've worked with SQL Server, there's been an ongoing debate about whether stored procedures or dynamically-generated SQL strings are the better choice for certain jobs.

Stored procedures are useful when you have a function that changes relatively little or has parameters easily encompassed within the syntax of a stored procedure. , on the other hand, is generally for quick-and-dirty work. It's also used when dealing with something that needs to be assembled almost entirely from scratch; For instance when the number and variety of parameters, the tables being queried, and so on may vary from statement to statement. But even then a stored procedure is suitable and in the long run the better choice.

The Rule…

Most SQL Server programmers try to consolidate their code into stored procedures for several reasons.

Performance. This is one of those "everyone knows this" facts, but it bears repeating. Stored procedures are cached and reused a lot more efficiently than dynamic SQL. Although there is some ongoing debate about the speed of stored procedures, there's not much question that in a large-scale multi-user application they show the best benefits.

Consistency. If you're a programmer, it's easier to reference a stored procedure to do something that may be invoked in multiple contexts than it is to rewrite the same SQL strings in different places (and possibly make a mistake doing so). It also makes it easier to incrementally upgrade code throughout a large application; the references to the underlying stored procedure can be changed much more gracefully. Trying to replace all the instances of a dynamically-generated SQL block throughout an application is a horror no one should ever have to deal with.

Security. Stored procedures provide a way to do granular security on certain functions, especially if your database is used by more th


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
SQL Server and .NET development
Microsoft releases new CTP of Oslo SDK
Perpetuum unveils database synchronizer for .NET 2.0
The CTE, the hierarchical query and SQL Server 2005
SQL Server 2005 recursive functions and the CTE
SQL Examiner Suite synchronizes data schema, sets
Book excerpt: ADO.NET and SQL Server 2005
DataDirect database drivers now support MySQL
Top .NET tips of 2007 (so far)
Addressing common SQL Server questions
Book Excerpt: The .NET Framework and SQL Server 2005

Microsoft SQL Server
Using the Visual Studio 2005 DataSet Designer to build a data access layer
The CTE, the hierarchical query and SQL Server 2005
SQL Server 2005 recursive functions and the CTE
Choose the right .NET data provider, optimize application performance
The fallacy of the data layer -- or, a new architectural model for software designs
Book excerpt: ADO.NET and SQL Server 2005
Addressing common SQL Server questions
Book Excerpt: The .NET Framework and SQL Server 2005
WinForms development using SQL Server 2005 and Visual Basic 2005
Configuring ASP.NET 2.0 apps to SQL Server 2005 databases

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


an one application at once. Dynamically-generated SQL statements are not really "owned" by anyone -- the only permissions in force are the user contexts used to run them in -- so it's harder to enforce security.

….And The Exception

There are several reasons why some people choose dynamic SQL over stored procedures. On closer inspection, though, the their decision stems from a lack of acquaintance for how many existing T-SQL functions can provide the same effects.

Quick-and-dirty deployment. This is probably the most commonplace reason for using . In the context of a given application, it's just easier and faster to throw together a SQL string and pass it to SQL Server rather than develop a stored procedure on the backend to do the same thing.

What's problematic is that if this "quick-and-dirty" approach turns into an application-wide habit — or mushrooms into a big one. What was just a convenient way to get things done now turns into a long-term maintenance nightmare. In the long run, any application that passes more than a few queries should have them replaced with manageable stored procedures.

Doing work on multiple databases or tables. Another common use (abuse?) of dynamic SQL I often see is constructing a series of T-SQL statements that each run against a different table or database. Another way to do this with multiple databases is to use a Microsoft stored procedure, sp_MSforeachdb, which accepts a T-SQL statement as a parameter and runs said statement on every database in the instance of SQL Server you're connected to. This is not a perfect solution since the procedure itself is officially undocumented and could cease to exist or stop working at some point.

Passing columns or tables as parameters. Some SQL statements simply cannot be rendered elegantly as a stored procedure. One scenario is a piece of SQL that has a column or table name passed to it as a parameter (or as part of a parameter), which isn't allowed in the context of a stored procedure.

A possible way to get around this is to create a stored procedure that works as a wrapper for the EXEC sp_executesql statement. In essence, use a stored procedure to generate dynamic SQL. This could even be used to dynamically create a stored procedure that works only on a specific table or column as needed, so it could be re-used in the future. One danger, however, is if you don't have the data sanitized beforehand, can become a possible vector for SQL injection attacks.

Many of the circumstances involving dynamically-passed parameters can be solved in the context of a stored procedure. A similar situation involves a dynamic sort condition, where a passed parameter would be used in an ORDER BY statement. If the number of possible parameters is relatively small, you could simply use an ORDER BY CASE statement with the variable to deal with this in a clean way. If you wanted to pass a parameter to select the TOP n rows of a query, you could use SET ROWCOUNT in the context of a stored procedure to achieve the same exact effect.

I should point out that if you constantly find yourself in a position where you're forced to pass table names or column names as parameters, the real culprit is probably not SQL Server's limitations but a data design that doesn't encompass your real needs. This is not always the case, but it happens often enough, where you may want to take a hard look at how your schema is put together to make sure you're not fighting the wrong battle. Do not try to fight against the odds of a badly-designed database and make it do work it was never intended to do.

About the author: Serdar Yegulalp is editor of the Windows Power Users Newsletter. Check it out for the latest advice and musings on the world of Windows network administrators -- and please share your thoughts as well!

More information on SearchSQLServer.com

  • Expert Advice: FAQ: SQL Server stored procedure how-tos
  • Tip: Upgrading stored procedures in SQL Server 2005
  • Guide: Don't expose interfaces that create dynamic SQL to the end user

    Rate this Tip
    To rate tips, you must be a member of SearchWinDevelopment.com.
    Register now to start rating these tips. Log in if you are already a member.




    DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



  • Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Site Map




    All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts