简体   繁体   English

SQL Server存储过程未返回值

[英]SQL Server stored procedure does not return value

I have a table with some data and I have written a stored procedure to return a count of the rows: 我有一个包含一些数据的表,并且编写了一个存储过程以返回行数:

ALTER PROCEDURE [dbo].[SP_User_Case_Answer_Found]
    @userID BIGINT,
    @visitID BIGINT
AS
    RETURN 
        SELECT COUNT(*) 
        FROM User_Case_Answer
        WHERE userID = @userID
          AND pvisitID = @visitID
GO

and when I execute it I get: 当我执行它时,我得到:

DECLARE @return_value int

EXEC    @return_value = [dbo].[SP_User_Case_Answer_Found]
        @userID = 26,
        @visitID = 5

SELECT  'Return Value' = @return_value
GO

But the return value = 0? 但是返回值= 0? I am sure the table contains data. 我确定该表包含数据。

Enclose the query in parenthesis. 将查询括在括号中。

CREATE PROCEDURE [dbo].[SP_User_Case_Answer_Found]
    @userID BIGINT,
    @visitID BIGINT
AS
   return (SELECT count (*) 
    FROM User_Case_Answer
    WHERE userID = @userID
    AND pvisitID = @visitID)

db<>fiddle 分贝<>小提琴

Remove the return from the SELECT statement in the stored procedure. 从存储过程中的SELECT语句中删除return It isn't needed. 不需要 You would use return if you are trying to short circuit and not return a result set or if you are returning a status code such as RETURN 1 . 如果试图短路而不返回结果集,或者返回诸如RETURN 1类的状态代码,则可以使用return

   ALTER PROCEDURE [dbo].[SP_User_Case_Answer_Found]
        @userID BIGINT
        ,@visitID BIGINT
    AS
           SELECT COUNT(*) 
           FROM User_Case_Answer
           WHERE userID = @userID
               AND pvisitID = @visitID;
    GO

You don't need to bind a return value when calling the procedure either. 您也不需要在调用过程时绑定返回值。

EXEC [dbo].[SP_User_Case_Answer_Found] @userID = 26, @visitID = 5

A return value parameter would be needed if you are using an output parameter. 如果使用输出参数,则需要返回值参数。 T-SQL RETURN T-SQL返回

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM