-1

我有一个查询,它将向我的表中插入一个数据,它应该返回一个值

我的存储过程

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 必须声明标量变量“@ReqID”。

Msg 137, Level 15, State 2, Line 3 必须声明标量变量“@ReqID”。

更新

是的,我做错了。我错过了声明输出参数。但是在我像下面这样执行之后

DECLARE @ReqID varchar(30)
EXECUTE FspCreateRequest @RequestDescription='descriotion',@CreatedBy='sa',@CreatedDate='2017-01-10',@Region='Jeddah',@ReqID out
PRINT 'RequestID'+ @ReqID 

仍然抛出如下错误

消息 119,第 15 层,状态 1,第 3 行

必须将参数号 5 和后续参数作为“@name = value”传递。使用“@name = value”形式后,所有后续参数必须以“@name = value”形式传递。

4

1 回答 1

1

这是正确答案

DECLARE @ReqID varchar(30);
EXEC FspCreateRequest @RequestDescription='descriotion',@CreatedBy='sa',@CreatedDate='2017-01-10',@Region='Mecca',@RequestID = @ReqID out;
PRINT 'RequestID'+ @ReqID ;

谢谢@DanGuzman

于 2017-04-29T20:23:26.220 回答