如何在我的场景中在存储过程中创建输出参数?
How to make an output parameter in Stored Procedure in my scenario?
我有一个查询,它将向我的 table 中插入一个数据,它应该 return 一个值
我的存储过程
ALTER PROC FspCreateRequest
@RequestDescription nvarchar(200),
@CreatedBy varchar(30),
@CreatedDate datetime,
@Region nchar(10),
@RequestID varchar(20) output
AS
BEGIN
DECLARE @maxRequestID AS varchar(20)
SELECT @maxRequestID=max(RequestID)
FROM [F_CheckRequest]
WHERE Region = @Region
IF (@maxRequestID IS Null or @maxRequestID = '')
BEGIN
IF(@Region = 'Jeddah')
BEGIN
INSERT INTO [F_CheckRequest
(RequestID,RequestDescription,CreateBy,CreatedDate,Region)
VALUES
('10001',@RequestDescription,@CreatedBy,@CreatedDate,@Region)
SELECT @RequestID = '10001'
END
ELSE
BEGIN
INSERT INTO [F_CheckRequest]
(RequestID,RequestDescription,CreateBy,CreatedDate,Region)
VALUES
('50001',@RequestDescription,@CreatedBy,@CreatedDate,@Region)
SELECT @RequestID = '50001'
END
END
ELSE
BEGIN
SET @maxRequestID=@maxRequestID+1;
INSERT INTO [F_CheckRequest]
(RequestID,RequestDescription,CreateBy,CreatedDate,Region)
VALUES
(@maxRequestID,@RequestDescription,@CreatedBy,@CreatedDate,@Region)
SELECT @RequestID = @maxRequestID
END
END
当我通过以下代码运行查询时
EXECUTE FspCreateRequest @RequestDescription='descriotion',@CreatedBy='sa',@CreatedDate='2017-01-10',@Region='Mecca',@ReqID out
PRINT 'RequestID'+ @ReqID
它抛出错误
Msg 137, Level 15, State 2, Line 2 Must declare the scalar variable "@ReqID".
Msg 137, Level 15, State 2, Line 3 Must declare the scalar variable "@ReqID".
已更新
是的,我错了。我错过了声明输出参数。但是在我像下面这样执行之后
DECLARE @ReqID varchar(30)
EXECUTE FspCreateRequest @RequestDescription='descriotion',@CreatedBy='sa',@CreatedDate='2017-01-10',@Region='Jeddah',@ReqID out
PRINT 'RequestID'+ @ReqID
仍然抛出如下错误
Msg 119, Level 15, State 1, Line 3
Must pass parameter number 5 and subsequent parameters as '@name = value'. After the form '@name = value' has been used, all subsequent parameters must be passed in the form '@name = value'.
这是正确答案
DECLARE @ReqID varchar(30);
EXEC FspCreateRequest @RequestDescription='descriotion',@CreatedBy='sa',@CreatedDate='2017-01-10',@Region='Mecca',@RequestID = @ReqID out;
PRINT 'RequestID'+ @ReqID ;
谢谢@DanGuzman
我有一个查询,它将向我的 table 中插入一个数据,它应该 return 一个值
我的存储过程
ALTER PROC FspCreateRequest
@RequestDescription nvarchar(200),
@CreatedBy varchar(30),
@CreatedDate datetime,
@Region nchar(10),
@RequestID varchar(20) output
AS
BEGIN
DECLARE @maxRequestID AS varchar(20)
SELECT @maxRequestID=max(RequestID)
FROM [F_CheckRequest]
WHERE Region = @Region
IF (@maxRequestID IS Null or @maxRequestID = '')
BEGIN
IF(@Region = 'Jeddah')
BEGIN
INSERT INTO [F_CheckRequest
(RequestID,RequestDescription,CreateBy,CreatedDate,Region)
VALUES
('10001',@RequestDescription,@CreatedBy,@CreatedDate,@Region)
SELECT @RequestID = '10001'
END
ELSE
BEGIN
INSERT INTO [F_CheckRequest]
(RequestID,RequestDescription,CreateBy,CreatedDate,Region)
VALUES
('50001',@RequestDescription,@CreatedBy,@CreatedDate,@Region)
SELECT @RequestID = '50001'
END
END
ELSE
BEGIN
SET @maxRequestID=@maxRequestID+1;
INSERT INTO [F_CheckRequest]
(RequestID,RequestDescription,CreateBy,CreatedDate,Region)
VALUES
(@maxRequestID,@RequestDescription,@CreatedBy,@CreatedDate,@Region)
SELECT @RequestID = @maxRequestID
END
END
当我通过以下代码运行查询时
EXECUTE FspCreateRequest @RequestDescription='descriotion',@CreatedBy='sa',@CreatedDate='2017-01-10',@Region='Mecca',@ReqID out
PRINT 'RequestID'+ @ReqID
它抛出错误
Msg 137, Level 15, State 2, Line 2 Must declare the scalar variable "@ReqID".
Msg 137, Level 15, State 2, Line 3 Must declare the scalar variable "@ReqID".
已更新
是的,我错了。我错过了声明输出参数。但是在我像下面这样执行之后
DECLARE @ReqID varchar(30)
EXECUTE FspCreateRequest @RequestDescription='descriotion',@CreatedBy='sa',@CreatedDate='2017-01-10',@Region='Jeddah',@ReqID out
PRINT 'RequestID'+ @ReqID
仍然抛出如下错误
Msg 119, Level 15, State 1, Line 3
Must pass parameter number 5 and subsequent parameters as '@name = value'. After the form '@name = value' has been used, all subsequent parameters must be passed in the form '@name = value'.
这是正确答案
DECLARE @ReqID varchar(30);
EXEC FspCreateRequest @RequestDescription='descriotion',@CreatedBy='sa',@CreatedDate='2017-01-10',@Region='Mecca',@RequestID = @ReqID out;
PRINT 'RequestID'+ @ReqID ;
谢谢@DanGuzman