T Sql 简明教程

T-SQL - Stored Procedures

MS SQL Server Stored procedure 可用于通过在数据库中存储相同内容节省编写代码时间,并通过传递参数获得所需输出。

The MS SQL Server Stored procedure is used to save time to write code again and again by storing the same in database and also get the required output by passing parameters.

Syntax

以下是存储过程创建的基本语法。

Following is the basic syntax of Stored procedure creation.

Create procedure <procedure_Name>
As
Begin
<SQL Statement>
End
Go

Example

可以将 CUSTOMERS 表视为包含以下记录。

Consider the CUSTOMERS table having the following records.

ID  NAME       AGE       ADDRESS            SALARY
1   Ramesh     32        Ahmedabad          2000.00
2   Khilan     25        Delhi              1500.00
3   kaushik    23        Kota               2000.00
4   Chaitali   25        Mumbai             6500.00
5   Hardik     27        Bhopal             8500.00
6   Komal      22        MP                 4500.00
7   Muffy      24        Indore             10000.00

以下命令是一个示例,它将从 Testdb 数据库中的 CUSTOMERS 表中获取所有记录。

Following command is an example which would fetch all records from the CUSTOMERS table in Testdb database.

CREATE PROCEDURE SelectCustomerstabledata
AS
SELECT * FROM Testdb.Customers
GO

以上命令将生成以下输出。

The above command will produce the following output.

ID  NAME       AGE       ADDRESS           SALARY
1   Ramesh     32        Ahmedabad         2000.00
2   Khilan     25        Delhi             1500.00
3   kaushik    23        Kota              2000.00
4   Chaitali   25        Mumbai            6500.00
5   Hardik     27        Bhopal            8500.00
6   Komal      22        MP                4500.00
7   Muffy      24        Indore            10000.00