您的位置:首页 > 数据库

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

2014-07-12 23:51 239 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: