在另一个存储过程中执行存储过程

时间:2019-04-28 18:19:28

标签: sql-server-2008 stored-procedures

我正在使用带有两个表(即[Card][Transaction])的银行数据库。

我创建了一个存储过程login,该存储过程作为输入pincardNumber的输入,并在输出参数中返回卡的有效性。现在,我创建了另一个存储过程withdraw,该存储过程获取输入pincardNumtransactionAmount并开始事务。我正在尝试使用p1程序来处理有效性部分。这是2个步骤

create procedure login
    @status int output, 
    @pin varchar(4), 
    @cnum varchar(20) 
as
begin
    if  exists (select * from [Card] where PIN = @pin AND cardNum = @cnum)
    begin
        set @status = 1
    end
    else
    begin 
        set @status = 0
    end
end

alter procedure WithDraw
    @pin varchar(4), 
    @cnum varchar(20), 
    @trAmount int
as
begin
    declare @m int

    exec login @pin = N'0234', @cnum = N'2324325423336', @status = @m OUTPUT

    if (select @m) = 1
    --if exists (select * from [Card] where cardNum = @cnum AND pin = @pin)
    --working fine with above statement
    begin
        if exists (select * from [Card] 
                   where cardNum = @cnum AND pin = @pin AND (balance > @trAmount))
        begin
            update [Card] 
            set balance = balance - @trAmount 
            where cardNum = @cnum AND pin = @pin

            declare @maxID int
            select @maxID = MAX(transId) from [Transaction]

            insert into [Transaction] 
            values (@maxID + 1, GETDATE(), @cnum, @trAmount, 1)

            print 'Transaction successful'
        end
        else
        begin
            select @maxID = MAX(transId) from [Transaction]

            insert into [Transaction] 
            values(@maxID + 1, GETDATE(), @cnum, @trAmount, 4)

            print 'Transaction unsuccessful! Insufficient balance in card'
        end
    end
    else
    begin
        print 'login failed'
    end
end

exec WithDraw @pin = N'1770', @cnum = N'1324327436569', @trAmount = 50000

但是,当我使用内部登录过程执行withdraw时,登录总是失败。当我执行无登录程序时,一切正常。任何帮助将不胜感激。

0 个答案:

没有答案