如何在SQL查询中执行SQL查询

时间:2015-12-04 14:13:49

标签: sql-server variables execute

我正在使用SQL Server 2014.我有一个名为“Withdraw”的SQL查询,它返回0或-1的值,具体取决于其操作是否成功。

我想要做的是创建第二个查询,它将执行第一个查询并将返回值放在变量中,以便我可以在新查询中的IF语句中使用它。

执行此操作的简单方法是复制另一个查询的代码,这可以完成(这不是一个查询的长度),但我想知道是否有更优雅的方法来执行此操作查询而不是复制它。

谢谢你们!

提取查询:

PROC [dbo].[withdrawl]
    (@AccountNum AS INT,
     @Amount AS INT)
AS
BEGIN
    set nocount on

    declare @rc as integer

    begin transaction
    begin try 
        IF @Amount < 20000
        BEGIN
            IF @Amount < ((SELECT crntbalance FROM tblAccounts WHERE acctNum = @AccountNum) + (SELECT overdraftsz FROM tblAccounts WHERE acctNum = @AccountNum))
            BEGIN
                UPDATE tblAccounts
                SET crntbalance -= @Amount
                WHERE acctNum = @AccountNum

                INSERT INTO tblTransactions (actNum, trnTypCod, amount) 
                VALUES (@AccountNum, 1, @Amount)

                UPDATE tblAccounts
                SET crntbalance += -5
                WHERE acctNum = @AccountNum

                INSERT INTO tblTransactions (actNum, trnTypCod, amount) 
                VALUES (@AccountNum, 5, 5)

                SET @rc = 0
            END
            ELSE
            BEGIN
                PRINT 'You do not have sufficient funds in your account to make this withdrawl.'
                SET @rc = -1
            END
        END
        ELSE
        BEGIN
            IF @Amount < ((SELECT crntbalance FROM tblAccounts WHERE acctNum = @AccountNum) + (SELECT overdraftsz FROM tblAccounts WHERE acctNum = @AccountNum))
            BEGIN
                UPDATE tblAccounts
                SET crntbalance -= @Amount
                WHERE acctNum = @AccountNum

                INSERT INTO tblTransactions (actNum, trnTypCod, amount) 
                VALUES (@AccountNum, 1, @Amount)

                SET @rc = 0
            END
            ELSE
            BEGIN
                PRINT 'You do not have sufficient funds in your account to make this withdrawl.'
                SET @rc = -1
            END
        END

        commit transaction
    END TRY
    BEGIN CATCH
        rollback transaction
        SET @rc = -1
    END CATCH

    BEGIN
        SELECT
            crntbalance AS 'New Balance'
        FROM 
            tblAccounts 
        WHERE 
            acctNum = @AccountNum
    END

    return @rc
END

新查询:

CREATE PROCEDURE Transfer 
     (@TakeAccount AS INT, @GiveAccount as INT, @amount as INT)
AS
BEGIN
    declare @rc as integer
    BEGIN TRY
        set @rc = EXECUTE dbo.withdrawl
        IF @rc = -1
            BEGIN

            END
        ELSE
            BEGIN
            END
    END TRY
    BEGIN CATCH
    END CATCH
END

2 个答案:

答案 0 :(得分:3)

这是使用存储过程的返回值设置变量的正确语法(假设已经声明了变量):

EXEC @rc = [schema].[StoredProc]
  {Parameters, if any}

答案 1 :(得分:1)

您可以将该查询转换为函数

,而不是使用PROC
CREATE FUNCTION [dbo].[Withdraw] 
(
    -- insert parameters here
)
RETURNS int
AS
BEGIN

    DECLARE @Result int

    -- insert processing here
    set @Result = -1


     return @Result
END

您可以在其他查​​询中使用其结果

SET @rc =  dbo.Withdraw()