•How to make stored procedures more flexible using input parameters•How to return information from stored procedures using output parametersCurrently, MySQL doesn’t support stored procedures (although it’s plannedfor future versions), and Microsoft DataBase (MDB) files have their own form of stored procedures, which are called, confusingly,
. Therefore, this chapterfocuses exclusively on using stored procedures within a Microsoft SQL Server 2000Desktop Engine (MSDE) database.
Why Stored Procedures?
In a nutshell, using stored procedures gives you the following benefits:
: Hard-coded queries on individual pages mean a string of SQLon each and every one of those pages. If you use the same query on severalpages and need to change the query, you have to make the changes in every page. Stored procedures make maintaining the site easier by having only onecopy of the query.
: Allowing direct access to the tables within the database to applica-tions, as you’ve already seen in Chapter 2, forces you to grant “too much” accessto the database. By using stored procedures, you only allow the user access tothe tables through stored procedures, and you can apply suitable controls.
: If you pass a query string to the server, you have to compile it before itcan be executed. If you pass the same query three times, the database has tocompile the query three times. Stored procedures are compiled when they’recreated, which is the end of it—all subsequent usage is with a speedy, pre-compiled piece of code. In addition, the database also optimizes the storedprocedure, by using information related to the structure of the database.
: SQL defines several flow-control structures that you’re familiar with from C# (such as the IF statement and WHILE and FOR loops), and thesearen’t available if you pass the SQL query directly from the page. With storedprocedures, you can perform all the flow control you need within the database,thus removing the data processing burden from within the page. A databaseis designed to process data—so you should take advantage of the database’snatural talent.
Reduced network traffic
: Stored procedures allow you to process the resultsat the database and return only the required results to the page.
3693_Ch09_FINAL 7/7/04 4:23 PM Page 328