SQL SERVER存储过程,参数默认值设置

Example1:

USE AdventureWorks2008R2;
GO
IF OBJECT_ID('Sales.uspGetSalesYTD', 'P') IS NOT NULL
    DROP PROCEDURE Sales.uspGetSalesYTD;
GO
CREATE PROCEDURE Sales.uspGetSalesYTD
@SalesPerson nvarchar(50) = NULL  -- NULL default value
AS
    SET NOCOUNT ON;

-- Validate the @SalesPerson parameter.
IF @SalesPerson IS NULL
BEGIN
   PRINT 'ERROR: You must specify the last name of the sales person.'
   RETURN
END
-- Get the sales for the specified sales person and
-- assign it to the output parameter.
SELECT SalesYTD
FROM Sales.SalesPerson AS sp
JOIN HumanResources.vEmployee AS e ON e.BusinessEntityID = sp.BusinessEntityID
WHERE LastName = @SalesPerson;
RETURN
GO

Example 2:

IF OBJECT_ID('dbo.my_proc', 'P') IS NOT NULL
    DROP PROCEDURE dbo.my_proc;
GO
CREATE PROCEDURE dbo.my_proc
    @first int = NULL,  -- NULL default value
    @second int = 2,    -- Default value of 2
    @third int = 3      -- Default value of 3
AS
    SET NOCOUNT ON;
    SELECT @first, @second, @third;
GO

你可能感兴趣的:(SQL SERVER存储过程,参数默认值设置)