| To read the article online, visit http://www.4GuysFromRolla.com/webtech/102300-1.shtml |
|
By Scott Mitchell We've all likely created ASP pages that contain dynamic SQL statements within the ASP pages... SQL statements that are constructed on the fly, its parameters based upon, usually, user input. For example, imagine that you had a database table that stored information on your company's employees. Perhaps you'd want to add some interface that allowed users to enter an employee's last name and have an ASP page that returned a list of employees with that last name. You would start by creating a form that allowed the user to enter a last name, redirecting the submitted form to an ASP page that built a dynamic SQL query based on the user's input. The code to build the dynamic SQL might look like:
Such dynamic SQL statements are, in my opinion, ugly, error-prone, and difficult to maintain. What happens if
a number of pages need to use this dynamic SQL statement? Are you going to cut and paste it into each page?
And if so, what happens when the
Dynamic SQL is also hard to read, especially if you have tons of ASP variables inserted into the string,
or you build up the string on a number of conditional statements. I've been guilty of creating quite lengthy
and horribly unreadable dynamic SQL strings, strings that were created over fifty or so lines of code, being
very dynamic, involving an assortment of Whenever I need to access a SQL Server database through an ASP page, I like to use a stored procedure for a number of reasons. (If you are unfamiliar with what, exactly, stored procedures are, or how to create them, I highly suggest you read Nathan Pond's article: Writing a Stored Procedure.) Stored procedures have the following advantages over in-script SQL statements:
Now, you may be thinking, "Yeah, I usually like to use stored procedures, but I need to execute dynamic SQL statements." Well, you can use dynamic SQL statements within a stored procedure! Using dynamic SQL in stored procedures does reduce one advantage of stored procedures: performance. What good's an execution plan when the stored procedure is dynamic? (For more on this topic, be sure to read: The Speed of Dynamic Queries!) However, there are still a number of other advantages that stored procedures offer, and hence, I argue, it is still worth creating stored procedures to handle dynamic SQL.
That being said, let's look at how we can use dynamic SQL statements within a stored procedure! SQL contains
a nice command,
Of course, this isn't a very practical use of the
Note that I used the double apostrophes to denote a single apostrophe in the dynamic SQL string. This pair of apostrophes creates a single, literal apostrophe in the dynamic SQL string...
At this point we've not added any value to our stored procedure. After all, we simply have a hard-coded SQL
statement being executed by the
|
| Article Information | |
| Article Title: | Using Dynamic SQL Statements in Stored Procedures |
| Article Author: | Scott Mitchell |
| Published Date: | Monday, October 23, 2000 |
| Article URL: | http://www.4GuysFromRolla.com/webtech/102300-1.shtml |
Copyright 2010 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers