|
SQL Server Home
SQL Server 2000 SQL Server 2008About SQL ServerSQL Server Editions SQL Server Management Studio Create A Database Create A Table Adding Data SQL Scripts Query Designer SQL Server Views Stored Procedures User Logins Server Roles Database Schemas Linked Servers Integration Services Summary FREE Hosting!With every domain name you register with ZappyHost, you get FREE hosting.$1.99 Domain NamesWith every new non-domain purchase thru ZappyHost, you get a domain name for only $1.99. |
SQL Server - Stored ProceduresNote that this tutorial was written for those using the 2000 edition of SQL Server (SQL Server 2000). For later versions of SQL Server, check out the SQL Server 2008 tutorial. Stored procedures are a powerful part of SQL Server. They can assist programmers and administrators greatly in working with the database configuration and its data. A stored procedure is a precompiled group of Transact-SQL statements, and is saved to the database (under the "Stored Procedures" node). Programmers and administrators can execute stored procedures either from the Query Analyzer or from within an application as required. Transact-SQL, which is based on SQL (Structured Query Language), is the programming language used to interface between applications and their databases. Transact-SQL is a relatively easy language to learn and I highly recommend becoming familiar with it. Benefits of Stored ProceduresHere are some key benefits in using stored procedures:
Creating a Stored ProcedureYou create stored procedures in the Query Analyzer using the CREATE PROCEDURE statement, followed by the code that makes up the stored procedure.
The following code creates a stored procedure called "MyStoredProcedure":
Once you run this code in the Query Analyzer, the stored procedure is created and appears under the "Stored Procedures" node. Modifying a Stored ProcedureIf you need to modify an existing stored procedure, you simply replace the CREATE with ALTER.
Running a Stored ProcedureYou can run a stored procedure by using EXECUTE or EXEC. For example, to run the above stored procedure, type the following:
If the stored procedure has spaces in its name, enclose it between double quotes:
If your stored procedure accepts any parameters, they are placed after the procedure name:
The following screenshot shows the results of running a stored procedure called "SalesByCategory" and passing it a parameter of "Beverages":
ParametersA parameter is a value that your stored procedure uses in order to perform it's task. When you write a stored procedure, you can specify the parameters that need to be supplied from the user. For example, if you write a stored procedure to select the address details about an individual, your stored procedure needs to know which individual to select. In this case, the user can provide an IndividualId or UserId to tell the stored procedure which individual to look up. System Stored ProceduresSQL Server includes a large number of system stored procedures to assist in database administration tasks. Many of the tasks you can perform via Enterprise Manager can be done via a system stored procedure. For example, some of the things you can do with system stored procedures include:
Enjoy this website?
Oh, and thank you for supporting Quackit! |
Need Content for your Website?Get unique, quality digital content for your website. You can even earn money by reselling it!Includes:
|