必须使用输入和输出参数声明sql ....中的标量变量

时间:2016-08-10 10:35:10

标签: sql sql-server

我得到的SP将USER_ID作为输入并从表中获取GROUP_ID,但在我尝试运行它时显示错误:

  

必须声明标量变量@Group_i

我的存储过程:

Alter PROCEDURE [dbo].[DisplayDetails] 

@User_Id int,
@Group_Id int OUTPUT

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

Begin
-- Insert statements for procedure here
SELECT 
@Group_Id=(select distinct(Group_Id) from input_files_updated)   
 from INPUT_FILES_updated where [User_Id]=@User_Id

END
END

GO

执行SP的查询:

Declare @Group_I int    

Execute [dbo].[DisplayDetails] @User_Id='3',    
@Group_Id=@Group_I out

为什么我收到此错误?

1 个答案:

答案 0 :(得分:1)

对你的一点点固定查询:

Alter PROCEDURE [dbo].[DisplayDetails] 
    @User_Id int,
    @Group_Id int OUTPUT
AS
BEGIN
SET NOCOUNT ON;

SELECT @Group_Id = (
    select top 1 Group_Id
    from INPUT_FILES_updated 
    where [User_Id]=@User_Id
)

RETURN  
END

我记得在SP中必须有一个单词RETURN。然后尝试:

Declare @Group_I int    

Execute [dbo].[DisplayDetails] @User_Id=3, @Group_Id=@Group_I output

SELECT @Group_I