T-SQL存储过程,OUTPUT参数给出错误

时间:2013-07-25 05:53:56

标签: sql tsql stored-procedures

我的存储过程定义为

create procedure p1
(
    @id INT,
    @name varchar(20) OUTPUT,
    @company varchar(20) OUTPUT
)
AS
     BEGIN
        select @name = name, @company = company from table1 where id = @id;
     END
GO

我用

来称呼它
DECLARE @name varchar(20), @company varchar(20), @id INT;
exec dbo.p1 @id=2, @name OUTPUT, @company OUTPUT;
select @name AS 'NAME', @company AS 'COMPANY'

但是我收到错误

'Must pass parameter number 2 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'.

4 个答案:

答案 0 :(得分:14)

按照错误消息中的说明进行操作:

DECLARE @name varchar(20), @company varchar(20), @id INT;
exec dbo.p1 @id=2, @name = @name OUTPUT, @company = @company OUTPUT;
select @name AS 'NAME', @company AS 'COMPANY'

替代方案 - 不要命名参数1:

DECLARE @name varchar(20), @company varchar(20), @id INT;
exec dbo.p1 2, @name OUTPUT, @company OUTPUT;
select @name AS 'NAME', @company AS 'COMPANY'

来自EXECUTE

  

@ parameter_name = value 表单一起使用时,参数名称和常量不必按照在模块中定义的顺序提供。但是,如果 @ parameter_name = value 表单用于任何参数,则必须将其用于所有后续参数。

(因为,显然,第一种用法可能是定义任何参数,因此它不能再假设应按顺序分配任何后续的参数)

答案 1 :(得分:3)

兄弟,我不确定您要选择的是什么,但也许您可以尝试以下查询:

CREATE PROCEDURE p1
(
@id INT,
@name varchar(20) OUTPUT,
@company varchar(20) OUTPUT
)
AS
 BEGIN
Set @name = 'name'
Set @company = 'company'
    select @name , @company from table1 where id = @id;
 END
GO

答案 2 :(得分:2)

更改为此将起作用

    DECLARE @name varchar(20), @company varchar(20), @id INT;
    select @id=2
    exec dbo.p1 @id, @name OUTPUT, @company OUTPUT;
    select @name AS 'NAME', @company AS 'COMPANY'

答案 3 :(得分:0)

以下是您问题的工作代码。

请尝试这种方式。它会正常工作。

<强>程序

alter procedure my_pro
@id int,
@name varchar(30) output,
@email varchar(50) output
as
begin
select @name=Name,@email=EmailID from Users where id=@id
end

<强>执行

declare @Newname varchar(40)
declare @Newemail varchar(40)
exec my_pro 2,@Newname output, @Newemail output
select @Newname 'Name', @Newemail 'Email'