Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Stored Procedures

Stored Procedures

Ratings: (0)|Views: 206 |Likes:
Published by Harish
Stored Procedures, web configuration ,need ,creation and security of stored procedures
Stored Procedures, web configuration ,need ,creation and security of stored procedures

More info:

Published by: Harish on Mar 14, 2010
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





Stored Procedures
Structured Query Language (SQL) queries against the databasein previous chapters, the one commonality is that the query is contained withinthe page where the execution will apply. Although you saw that this works effec-tively with simple SELECT, INSERT, UPDATE, and DELETE queries, you’ll soonsee that formulating a query this way isn’t always a suitable solution.SQL provides you with a better solution for executing queries against a databasethat’s beneficial for both runtime performance and design-time development—stored procedures. Put simply, a
stored procedure 
is a construct to store queries onthe server, so the same query is available for any application that wants to use it.The queries you’ve looked at so far have always been
queries that per-formed only one action; for example, you’ve performed SELECT queries that returnone set of results and INSERT queries that insert data into a single table. When youstep into more complex data (and a need for more complex results), you’ll find this way of working constrictive.Stored procedures can contain just single SQL queries, and when they do so,they’re naturally direct replacements for
queries. However, stored proce-dures are a powerful tool that can deliver much more than this. As you’ll shortly see, they can increase the performance of your queries and make maintaining  Web sites a whole lot easier. They may contain multiple queries and exploit thepower of SQL itself, which has more facility than you’d think, as it’s a maturelanguage in its own right.
We won’t present all the intricacies of SQL and all the different commands that are supported.You can find an introduction to SQL in Appendix B.
In this chapter, you’ll look at the following:Why you need stored proceduresHow to create stored procedures and give users access to themHow to execute stored procedures through a Command objectHow you can alter and delete stored procedures you’ve already created
3693_Ch09_FINAL 7/7/04 4:23 PM Page 327
How to make stored procedures more flexible using input parametersHow 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.
Flow control
: 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.
Chapter 9 
3693_Ch09_FINAL 7/7/04 4:23 PM Page 328
Creating Stored Procedures
To create a stored procedure, you must use a Data Definition Language (DDL)query. You’ll look at DDL queries in more detail in Chapter 10 and see several dif-ferent DDL queries, but for now all you need to know is that DDL queries allow  you to change the structure of the database.The DDL query to create stored procedures is CREATE PROCEDURE, which,in its simplest form, is as follows:
CREATE PROCEDURE <name>AS<queries>
 You give the stored procedure a name, and you can include whatever SQLqueries you want after the
Try It Out: Creating a Stored Procedure
This introduction to stored procedures will start with using SQL Web Data Admin-istrator to create a stored procedure that returns all the publishers from the data-base. You’ll see here that the tools you have at your disposal make it quite easy tocreate stored procedures. Follow these steps:1.Open SQL Web Data Administrator, and log into the (local)\BAND serverusing the sa account.2.Select the Books database, and then select the Stored Procedures optionin the left menu. This will give a list of all the stored procedures in thedatabase, currently none, as shown in Figure 9-1.3.Click the Create New Stored Procedure option, and enter
as the name of the stored procedure.4.Click the Create button to proceed to the page that allows you to enter thestored procedure (see Figure 9-2).5.Enter the following after the precompleted stored procedure declaration:
SELECT PublisherNameFROM PublisherORDER BY PublisherName
Stored Procedures 
3693_Ch09_FINAL 7/7/04 4:23 PM Page 329

Activity (23)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads
withrj liked this
kmdasari liked this
kshetra_1 liked this
withrj liked this
withrj liked this
sagerofgyan liked this
radzimus10 liked this

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->