1、创建存储过程:
CREATE PROCEDURE GetEmployee
@EmployeeID INT,
@FirstName VARCHAR(50) OUTPUT,
@LastName VARCHAR(50) OUTPUT
AS
BEGIN
SELECT @FirstName = FirstName, @LastName = LastName
FROM Employees
WHERE EmployeeID = @EmployeeID
END
在例子中,创建了一个名为GetEmployee的存储过程,接受一个@EmployeeID参数,并输出@FirstName和@LastName。
2、调用存储过程:
DECLARE @FirstName VARCHAR(50)
DECLARE @LastName VARCHAR(50)
EXEC GetEmployee @EmployeeID = 1,
@FirstName = @FirstName OUTPUT,
@LastName = @LastName OUTPUT
SELECT @FirstName AS FirstName, @LastName AS LastName
代码中,首先声明了两个变量@FirstName和@LastName,然后通过EXEC命令调用存储过程GetEmployee,并将输出参数@FirstName和@LastName赋值给这两个变量。最后,通过SELECT语句将这两个变量的值输出。
发表评论