简体   繁体   English

存储过程问题-无法同时获得两个返回值

[英]Stored Procedure Issue - Can't get both return values

I have a stored procedure as seen below: 我有一个存储过程,如下所示:

ALTER PROCEDURE [dbo].[CreateNewLeague]
    -- Add the parameters for the stored procedure here
    @UserId uniqueidentifier,
    @LeagueName VARCHAR(256),
    @leagueId Int OUTPUT,
    @teamId Int OUTPUT

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

    SELECT @teamId = [teamID] FROM [UserTeam] WHERE (userID = @UserId)

    RETURN @teamID
END

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

    INSERT INTO League([leagueAdminID], [leagueName]) VALUES (@UserId, @LeagueName)
    SELECT SCOPE_IDENTITY()

    Set @leagueId = SCOPE_IDENTITY()

    RETURN @leagueId
END



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

    INSERT INTO LeagueTeam([leagueID], [teamID]) VALUES (@leagueId, @teamID)

END

My Issue is that I cannot get both return values when I execute the SPROC, I only ever get the return value of the one I put first. 我的问题是,我在执行SPROC时不能同时获得两个返回值,而只能获得我首先输入的返回值。 They both work when they are put first, but the second returns a value of NULL. 当它们都放在第一位时它们都起作用,但是第二个都返回NULL值。

Any ideas what I am doing wrong? 有什么想法我做错了吗?

Thanks in advance 提前致谢

"Return" stops executing the stored procedure, so the second part is never executed. “返回”将停止执行存储过程,因此永远不会执行第二部分。 Leave out the "return" statements and read the output vars, you are already setting their values. 省略“ return”语句并读取输出变量,您已经在设置它们的值了。

if you are using the output parameter then do not use the return only set the variables. 如果使用输出参数,则不要使用return设置变量。 ex. 例如 SET @teamID = 10 SET @teamID = 10

设置值而不是返回。

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

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