SQL2000 Stored Procedure Basics

Source: Internet
Author: User

Concept of Stored Procedure
SQL Server provides a method to centralize some fixed operations by the SQL Server database server to implement a task. This method is a stored procedure.
A stored procedure is a set of pre-compiled SQL statements and optional control flow statements. It is stored in a database and can be executed by an application through one call, it also allows users to declare variables, conditional executions, and other powerful programming functions.
SQL Server has two types of stored procedures: stored procedures provided by the system and custom stored procedures.

Stored procedures can be used for any purpose of using SQL statements. They have the following advantages:
You can execute a series of SQL statements in a single stored procedure.
You can reference other stored procedures from your stored procedures, which simplifies a series of complex statements.
The storage process is compiled on the server when it is created. Therefore, it runs faster than a single SQL statement and reduces the network communication burden.
Higher security.
Create a stored procedure

You can create a stored procedure in SQL Server in three ways:
① Use the create Stored Procedure Wizard to create a stored procedure.
② Create a stored procedure using SQL Server Enterprise Manager.
③ Use the create procedure command in the transact-SQL statement to create a stored procedure.

The following describes how to use the create procedure command in a Transact-SQL statement to create a stored procedure.
Before creating a stored procedure, consider the following:
① The create procedure statement and other SQL statements cannot be combined into a single batch.
② Stored procedures can be nested, and the maximum depth of nesting cannot exceed 32 layers.
③ By default, the stored procedure creation permission belongs to the database owner, who can grant this permission to other users.
④ A stored procedure is a database object and its name must comply with the identifier rules.
⑤ You can only create a stored procedure in the current database.
⑥ The maximum size of a stored procedure is 128 M.

The syntax for using CREATE procedure to create a stored procedure is as follows:

Quote:
Create proc [edure] procedure_name [; number] [; number]
[{@ Parameter data_type}
[Varying] [= default] [Output]
] [,... N]
{Recompile | encryption | recompile, encryption}]
[For replication]
As SQL _statement [... n]

Syntax parameters for creating a stored procedure using CREATE procedure are as follows:

Procedure_name: Specifies the name of the stored procedure to be created.
Number: this parameter is an optional integer used to group stored procedures with the same name, so that the same group of processes can be removed with a drop procedure statement.
@ Parameter: parameters in the process. One or more parameters can be declared in the create procedure statement.
Data_type: Specifies the Data Type of a parameter.
Varying: used to specify the result set supported by the output parameter.
Default: Specifies the default value of a parameter.
Output: indicates that this parameter is a return parameter.

For example, create a simple Stored Procedure productinfo to retrieve product information.
Use northwind
If exists (Select name from sysobjects
Where name = 'productinfo' and type = 'P ')
Drop procedure productinfo
Go

Create procedure productinfo
As
Select * from products
Go
Execute the stored procedure using the following SQL statement: Execute productinfo
You can retrieve the product information.

Execute the Stored Procedure

You can run the Execute Command to directly execute the stored procedure. The syntax format is as follows:
[[Exec [ute]
{[@ Return_status =]
{Procedure_name [; number] | @ procedure_name_var} [[@ parameter =] {value | @ variable [Output] | [Default]}
[,... N]
[With recompile]

 

Run the Execute Command to pass a single parameter. It executes the showind stored procedure and takes titles as the parameter value. The showind stored procedure requires a parameter (@ tabname), which is the name of a table. The program list is as follows:
Exec showind titles
Of course, variables can be explicitly named during execution:
Exec showind @ tabname = titles
If this is the first statement in an iSQL script or batch processing, the exec statement can be omitted:
Showind titles or showind @ tabname = titles

 

The following example uses the default parameter
Use northwind
Go
Create procedure insert_products_1
(@ Supplierid_2 int,
@ Categoryid_3 int,
@ Productname_1 nvarchar (40) = 'None ')
As insert into products
(Productname, supplierid, categoryid)
Values
(@ Productname_1, @ supplierid_2, @ categoryid_3)
Go
Exec insert_products_1 1, 1
Select * from products where supplierid = 1 and categoryid = 1
Go

The following example uses response parameters
Use northwind
Go
Create procedure query_products
(@ Supplierid_1 int,
@ Productname_2 nvarchar (40) Output)
As
Select @ productname_2 = productname from products
Where supplierid = @ supplierid_1

Run the stored procedure to query the product name with supplierid 1:
Declare @ Product nvarchar (40)
Exec query_products 1, @ product output
Select 'product name' = @ Product
Go

View stored procedures
After a stored procedure is created, its name is stored in the system table sysobjects, and its source code is stored in the system table syscomments. You can use the Enterprise Manager or system stored procedure to view the stored procedure you have created.

Use the Enterprise Manager to view the stored procedure created by the user

In Enterprise Manager, open the specified server and database items, select the database for which you want to create a stored procedure, and click the Stored Procedure folder. All stored procedures of the database are displayed in the right-side dialog box. Right-click the stored procedure you want to view and select the attribute option from the shortcut menu to view the source code of the stored procedure.

Use the system stored procedure to view the stored procedure created by the user

The system stored procedures and syntax are as follows:
Sp_help: displays the parameters and data types of stored procedures.
Sp_help [[@ objname =] Name]
The parameter name is the name of the stored procedure to be viewed.
Sp_helptext: displays the source code of a stored procedure.
Sp_helptext [[@ objname =] Name]
The parameter name is the name of the stored procedure to be viewed.
Sp_depends: used to display database objects related to stored procedures
Sp_depends [@ objname =] 'object'
The parameter object is the name of the stored procedure for viewing dependencies.
Sp_stored_procedures: Used to return the list of stored procedures in the current database

 

 

Modify Stored Procedure

The stored procedure can be changed based on user requirements or the definition of the base table. The alter procedure statement can be used to change the previously created procedure by executing the create procedure statement without changing the permissions or affecting the stored procedure or trigger. The syntax format is as follows:
Alterproc [edure] procedure_name [; number]
[{@ Parameterdata_type}
[Varying] [= default] [Output] [,... n] [
{Recompile | encryption | recompile, encryption}]
[For replication]
As
SQL _statement [... n]

Renaming and deleting stored procedures

1. Rename the Stored Procedure
To modify the name of a stored procedure, use the system stored procedure sp_rename. The syntax format is as follows:
Sp_rename original stored procedure name, new stored procedure name
You can also modify the name of a stored procedure through the Enterprise Manager.

 

Delete stored procedure

You can use the drop command to delete stored procedures. The drop command can delete one or more stored procedures or stored procedure groups from the current database. The syntax is as follows:
Drop procedure {procedure }[,... N]
Of course, you can also easily delete stored procedures using the Enterprise Manager.

 

Recompilation of Stored Procedures

After we use a stored procedure, we may have to add data columns to the table or add new indexes to the table for some reason, thus changing the logical structure of the database. In this case, you need to re-compile the stored procedure. SQL Server provides three methods to re-compile the stored procedure:
1. Set re-compilation when creating a stored procedure
Syntax format: Create procedure procedure_name with recompile as SQL _statement
2. Set re-compilation when executing the Stored Procedure
Syntax format: Execute procedure_name with recompile
3. Set re-compilation by using the system stored procedure
Syntax format: exec sp_recompile object

System stored procedures and Extended Stored Procedures

1. system stored procedures
The system stored procedures are stored in the master database and prefixed with SP _. They are mainly used to obtain information from the system table and help the system administrator manage SQL Server, it is convenient for users to view database objects. For example, sp_help, sp_helptext, and sp_helptext, which are used to view information about database objects.

2. extended storage process:
The extended stored procedure is prefixed with XP _. It is part of the open data service layer of the relational database engine. It enables users to dynamically connect to the library (DLL) the functions contained in the file implement the logic, and thus extend the functions of the transact-SQL statement. You can call these functions from the transact-SQL statement as you call the transact-SQL process.
For example, the stored procedure xp_cmdshell is used to execute a specified command string for an operating system shell and return any output as text.
Run the Code:
Use master
Exec xp_cmdshell 'dir *. EXE'
The execution result returns the text information of the file in the system directory.

Finally, we will give you an example:

Quote:

 

If exists (select * From sysobjects where name = 'more _ than_total 'and type = 'P ')
Drop procedure more_than_total
Go
Create procedure more_than_total
@ Total money = 0
As
Declare @ amount smallint
Begin
Select distinct
P. productname,
S. contactname,
P. unitprice

From products P inner join [Order Details] O
On P. productid = O. productid inner join suppliers s
On P. supplierid = S. supplierid
Where o. productid in
(Select productid
From [Order Details]
Group by productid
Having sum (quantity * unitprice)> @ total
)

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/renzhaoqiang/archive/2009/08/21/4468848.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.