简体   繁体   English

如何在存储过程中返回语句

[英]How to return a statements in stored procedure

This is my procedure : 这是我的程序:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[uspChangeMemberRole]
    @TeamID INT,
    @MemberID INT,
    @MemberRole TINYINT
AS 
BEGIN
    SET NOCOUNT ON;

    UPDATE [dbo].[TeamMember] 
    SET MemberRole = @MemberRole 
    WHERE TeamID = @TeamID 
      AND MemberID = @MemberID;

    RETURN @MemberRole;
END

When I execute this procedure I want to return "MemberRole Successfully Changed". 当我执行此过程时,我想返回“ MemberRole成功更改”。

Please give me a solution how to return 请给我一个解决方案如何退货

Use Row Count to check if there are affected rows (identified if update is successful) then select a string. 使用行数检查是否有受影响的行(确定更新是否成功),然后选择一个字符串。

RowCount - Returns the number of rows affected by the last statement. RowCount-返回受最后一条语句影响的行数。

IF @@ROWCOUNT > 0
BEGIN
   SELECT 'Member Role Successfully Chnaged';
END

And I think this statement is not necessary since this is an input parameter, you can get outside the proc. 而且我认为该语句不是必需的,因为这是一个输入参数,您可以在proc之外使用。

return @MemberRole;

Change your return to select, and replace the datatype of your output parameter with varchar. 将返回值更改为select,然后将输出参数的数据类型替换为varchar。

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    ALTER PROCEDURE [dbo].[uspChangeMemberRole]
    @TeamID INT,
    @MemberID INT,
    @MemberRole VARCHAR(100) OUTPUT
    AS BEGIN

    SET NOCOUNT ON;

    UPDATE [dbo].[TeamMember] SET MemberRole=@MemberRole WHERE TeamID=@TeamID AND MemberID=@MemberID;

    SET @MemberRole = 'MemberRole Successfully Changed.'

    SELECT @MemberRole AS 'RETURN';

I am adding some of the concept that will help you to understand how Procedure's will return values. 我要添加一些概念,以帮助您了解Procedure的返回值的方式。

  1. If you want to return something from SQL Server to Frontend, Just use " select " statement. 如果要将某些内容从SQL Server返回到Frontend,只需使用“ select ”语句。
  2. You have to use proper command from frontend like you want to return scalar value, or query. 您必须从前端使用正确的命令,例如要返回标量值或查询。

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

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