建立並呼叫儲存過程

可以通過資料庫管理 GUI( SQL Server 示例 )或通過 SQL 語句建立儲存過程,如下所示:

-- Define a name and parameters
CREATE PROCEDURE Northwind.getEmployee
    @LastName nvarchar(50),   
    @FirstName nvarchar(50)   
AS   

-- Define the query to be run
SELECT FirstName, LastName, Department  
FROM Northwind.vEmployeeDepartment
WHERE FirstName = @FirstName AND LastName = @LastName  
AND EndDate IS NULL;  

呼叫程式:

EXECUTE Northwind.getEmployee N'Ackerman', N'Pilar';

-- Or  
EXEC Northwind.getEmployee @LastName = N'Ackerman', @FirstName = N'Pilar';  
GO  

-- Or  
EXECUTE Northwind.getEmployee @FirstName = N'Pilar', @LastName = N'Ackerman';  
GO