例 3 – 带输入和输出参数的存储过程
这个例子中既有输入参数也有输出参数。在存储过程中查询后得到的ContactID可以利用输出参数进行回传。回传参数用户查询人员表中的其他字段,如ContactID、FirstName、LastName以及这个人的任何地址记录。
首先,我们按如下方式修改存储过程uspGetContact。其次,运行下面代码执行该存储过程。最后,基于传回的值可以查询人员的姓名和地址等信息。
ALTER PROCEDURE uspGetContact @LastName NVARCHAR(50), @ContactID INT output
AS
SELECT TOP 1 @ContactID = c.ContactID
FROM HumanResources.Employee a
INNER JOIN HumanResources.EmployeeAddress b ON a.EmployeeID = b.EmployeeID
INNER JOIN Person.Contact c ON a.ContactID = c.ContactID
INNER JOIN Person.Address d ON b.AddressID = d.AddressID
WHERE c.LastName = @LastName
存储过程修改后,运行如下代码将执行该存储过程。如果ContactID有值,就会返回人员和地址信息。
DECLARE @ContactID INT
SET @ContactID = 0
EXEC uspGetContact @LastName='Smith', @ContactID=@ContactID OUTPUT
IF @ContactID <> 0
BEGIN
SELECT ContactID, FirstName, LastName
FROM Person.Contact
WHERE ContactID = @ContactID
SELECT d.AddressLine1, d.City, d.PostalCode
FROM HumanResources.Employee a
INNER JOIN HumanResources.EmployeeAddress b ON a.EmployeeID = b.EmployeeID
INNER JOIN Person.Contact c ON a.ContactID = c.ContactID
INNER JOIN Person.Address d ON b.AddressID = d.AddressID
WHERE c.ContactID = @ContactID
END
[1] [2] [3] [4] [5] <
查询结果如下:
例 4 – 使用RAISERROR 语句的存储过程
在这个例子中,将把例3中的2个步骤整合到一个存储过程中。存储过程的第一步是取得ContactID,第二步是查询人员的姓名和地址信息。然后添加RAISERROR语句,如果查询不到记录则返回错误信息。
RAISERROR语句可以用来控制应用程序处理无数据或其它错误的方式。将该存储过程执行2次,看看能找到数据和不能找到数据的情况下各返回什么。
ALTER PROCEDURE uspGetContact @LastName NVARCHAR(50)
AS
DECLARE @ContactID INT
SELECT TOP 1 @ContactID = c.ContactID
FROM HumanResources.Employee a
INNER JOIN HumanResources.EmployeeAddress b ON a.EmployeeID = b.EmployeeID
INNER JOIN Person.Contact c ON a.ContactID = c.ContactID
INNER JOIN Person.Address d ON b.AddressID = d.AddressID
WHERE c.LastName = @LastName
IF @@ROWCOUNT > 0
BEGIN
SELECT ContactID, FirstName, LastName
FROM Person.Contact
WHERE ContactID = @ContactID
SELECT d.AddressLine1, d.City, d.PostalCode
FROM HumanResources.Employee a
INNER JOIN HumanResources.EmployeeAddress b ON a.EmployeeID = b.EmployeeID
INNER JOIN Person.Contact c ON a.ContactID = c.ContactID
INNER JOIN Person.Address d ON b.AddressID = d.AddressID
WHERE c.ContactID = @ContactID
END
ELSE
BEGIN
RAISERROR ('No record found',10,1)
END
EXEC uspGetContact @LastName='Walters'
查询结果如下:
例 5 -调用存储过程的存储过程
这个例子中有2个存储过程。第一个存储过程uspFindContact查找第一条带有地址信息的记录并将ContactID返回给调用它的存储过程,然后显示人员和地址信息。
CREATE PROCEDURE uspFindContact @LastName NVARCHAR(50), @ContactID INT output
AS
SELECT TOP 1 @ContactID = c.ContactID
FROM HumanResources.Employee a
INNER JOIN HumanResources.EmployeeAddress b ON a.EmployeeID = b.EmployeeID
INNER JOIN Person.Contact c ON a.ContactID = c.ContactID
INNER JOIN Person.Address d ON b.AddressID = d.AddressID
WHERE c.LastName = @LastName
下面的代码对uspGetContact做了一点修改:调用uspFindContact并返回结果集。
ALTER PROCEDURE uspGetContact @LastName NVARCHAR(50)
AS
DECLARE @ContactID INT
SET @ContactID = 0
EXEC uspFindContact @LastName=@LastName, @ContactID=@ContactID OUTPUT
IF @ContactID <> 0
BEGIN
SELECT ContactID, FirstName, LastName
FROM Person.Contact
WHERE ContactID = @ContactID
SELECT d.AddressLine1, d.City, d.PostalCode
FROM HumanResources.Employee a
INNER JOIN HumanResources.EmployeeAddress b ON a.EmployeeID = b.EmployeeID
INNER JOIN Person.Contact c ON a.ContactID = c.ContactID
INNER JOIN Person.Address d ON b.AddressID = d.AddressID
WHERE c.ContactID = @ContactID
END
ELSE
BEGIN
RAISERROR ('No record found',10,1)
END
EXEC uspGetContact @LastName='Walters'
[1] [2] [3] [4] [5] <
第一步查询结果如下:
例 6 -带评论的存储过程
最后这个例子基于存储过程uspGetContact,并增加了评论功能。展示了如何在一个存储过程中使用评论。存储过程中的评论有2种实现方式:1.使用-- 2.使用/*来作为评论的开始,以*/作为结束。其余部分和上面的例子一样。
ALTER PROCEDURE uspGetContact @LastName NVARCHAR(50)
AS
/* This is a sample stored procedure to show
how comments work within a stored procedure */
-- declare variable
DECLARE @ContactID INT
-- set variable value
SET @ContactID = 0
-- execute stored proc and return ContactID value
EXEC uspFindContact @LastName=@LastName, @ContactID=@ContactID OUTPUT
-- if ContactID does not equal 0 then return data else return error
IF @ContactID <> 0
BEGIN
SELECT ContactID, FirstName, LastName
FROM Person.Contact
WHERE ContactID = @ContactID
SELECT d.AddressLine1, d.City, d.PostalCode
FROM HumanResources.Employee a
INNER JOIN HumanResources.EmployeeAddress b ON a.EmployeeID = b.EmployeeID
INNER JOIN Person.Contact c ON a.ContactID = c.ContactID
INNER JOIN Person.Address d ON b.AddressID = d.AddressID
WHERE c.ContactID = @ContactID
END
ELSE
BEGIN
RAISERROR ('No record found',10,1)
END
这些都是非常简单的例子,通过这些例子,希望能让大家知道:创建一个SQL Server的存储过程并不难。如果能够在查询窗口或者应用程序中得心应手的使用查询语句,那么按照上面的例子,也就能够非常容易的掌握存储过程的使用。
[1] [2] [3] [4] [5] <