original link
http://www.codeproject.com/Articles/126898/Sql-Server-How-to-write-a-Stored-procedure-in-Sql
預存程序在sqlserver 中被定義成一組有邏輯關係的sql語句 ,他們實現了一些特定的任務。使用預存程序有很多好處。最主要的優點是提高資料庫效能。
還有如下好處
- 減少了送達資料庫的資訊量,這在網路頻寬有限的情況下是很重要的 。如果將一個有迴圈的sql執行語句通過網路到資料庫執行。執行沒有返回之前網路連接斷掉或者rollback語句沒有得到執行都會產生問題
- 預存程序建立好之後只要編譯一次。但sql語句每次送達執行時都需要編譯一次儘管每次送達執行的sql語句都一樣
- 提高了sql代碼的複用性,因為在多個使用者端可以調用同一份處理過程避免每次書寫相同的sql語句。也減少了開發的時間
- 有助於提高安全性,我們可以給指定的使用者授予某個預存程序的許可權從而避免把預存程序中涉及的表的許可權的授予
- 有時候用預存程序來封裝商業邏輯更安全,如果需要變化只在server端即可更改不需要對web端做任何改動
假設有一張表叫tbl_Students 結構如下:
CREATE TABLE tbl_Students( [Studentid] [int] IDENTITY(1,1) NOT NULL, [Firstname] [nvarchar](200) NOT NULL, [Lastname] [nvarchar](200) NULL, [Email] [nvarchar](100) NULL)
我們添加以下資料到上表
insert into tbl_Students n(Firstname.Lastname,email)
values('Vivek', 'Johari', 'vivek@abc.com')Insert into tbl_Students (Firstname, lastname, Email) Values('Pankaj', 'Kumar', 'pankaj@abc.com')Insert into tbl_Students (Firstname, lastname, Email) Values('Amit', 'Singh', 'amit@abc.com')Insert into tbl_Students (Firstname, lastname, Email) Values('Manish', 'Kumar', 'manish@abc.comm')
Insert into tbl_Students (Firstname, lastname, Email) Values('Abhishek', 'Singh', 'abhishek@abc.com')
現在我們開始寫預存程序,第一步是建立預存程序語句
Create Procedure Procedure-name (Input parameters ,Output Parameters (If required))AsBegin Sql statement used in the stored procedureEnd
我們來建立一個預存程序以studentid 為參數 返回studentname 內容如下
/* Getstudentname is the name of the stored procedure*/Create PROCEDURE Getstudentname(@studentid INT --Input parameter , Studentid of the student )ASBEGINSELECT Firstname+' '+Lastname FROM tbl_Students WHERE studentid=@studentid END
我們還可以把student name 作為output 參數 傳出 代碼如下
/* GetstudentnameInOutputVariable is the name of the stored procedure whichuses output variable @Studentname to collect the student name returns by thestored procedure*/Create PROCEDURE GetstudentnameInOutputVariable(@studentid INT, --Input parameter , Studentid of the student@studentname VARCHAR(200) OUT -- Out parameter declared with the help of OUT keyword)ASBEGINSELECT @studentname= Firstname+' '+Lastname FROM tbl_Students WHERE studentid=@studentidEND
在sqlserver 中 可以通過修改關鍵字來修改其內容。 假如我們想加入email address 資訊在上述預存程序中,就得再引入一個output 參數 如下
/* Stored Procedure GetstudentnameInOutputVariable is modified to collect theemail address of the student with the help of the Alert Keyword*/ Alter PROCEDURE GetstudentnameInOutputVariable(@studentid INT, --Input parameter , Studentid of the student@studentname VARCHAR (200) OUT, -- Output parameter to collect the student name@StudentEmail VARCHAR (200)OUT -- Output Parameter to collect the student email)ASBEGINSELECT @studentname= Firstname+' '+Lastname, @StudentEmail=email FROM tbl_Students WHERE studentid=@studentidEND
注意 並不是所有的預存程序都需要寫入傳回值 ,例如一些插入 刪除 更新的語句如下
/*This Stored procedure is used to Insert value into the table tbl_students. */Create Procedure InsertStudentrecord( @StudentFirstName Varchar(200), @StudentLastName Varchar(200), @StudentEmail Varchar(50)) As Begin Insert into tbl_Students (Firstname, lastname, Email) Values(@StudentFirstName, @StudentLastName,@StudentEmail) End
Sqlserver中執行預存程序
sqlserver中的預存程序可以用execute 或exec 來執行。例如執行預存程序Getstudentname
Execute Getstudentname 1Exec Getstudentname 1
執行,帶有output參數的預存程序,首先我們要聲明變數來容納參數,例如
Declare @Studentname as nvarchar(200) -- Declaring the variable to collect the StudentnameDeclare @Studentemail as nvarchar(50) -- Declaring the variable to collect the StudentemailExecute GetstudentnameInOutputVariable 1 , @Studentname output, @Studentemail outputselect @Studentname,@Studentemail -- "Select" Statement is used to show the output from Procedure
結 束語
我們可以說儲過程不但可以增強代碼的可複用執行,還可以通過減少網路資料來提高資料庫效能!