篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql MS SQL存储过程相关的知识,希望对你有一定的参考价值。
-- Simple parameterized procedure
CREATE PROCEDURE HumanResources.uspGetEmployeesTest2
@LastName NVARCHAR(50) = '',
@FirstName NVARCHAR(50)
AS
SET NOCOUNT ON;
SELECT FirstName,
LastName,
Department
FROM HumanResources.vEmployeeDepartmentHistory
WHERE FirstName = @FirstName
AND LastName = @LastName
GO
-- EXECUTION
HumanResources.uspGetEmployeesTest2 'Chen', 'John'
-- OR
EXECUTE HumanResources.uspGetEmployeesTest2
@FirstName= 'John',
@LastName ='Chen'
-- Stored procedure with an output parameter
CREATE PROC uspGetLastProductName
@ProductName Nvarchar(50) OUTPUT
AS
BEGIN
SELECT TOP 1 @ProductName= Name
FROM Production.Product
ORDER BY ProductID DESC;
END
-- EXECUTION
DECLARE @Result Nvarchar(50)
EXEC uspGetLastProductName @Result OUTPUT;
PRINT @Result
-- Stored procedure with Return value
CREATE PROC uspProductExists
@ProductName NVarchar(50)
AS
IF EXISTS( SELECT 1
FROM [Production].[Product]
WHERE Name = @ProductName)
RETURN 1;
ELSE
RETURN -1;
GO
-- EXECUTION
DECLARE @ReturnCode INT
EXEC @ReturnCode = uspProductExists 'Blade'
PRINT @ReturnCode;