使用存储过程借记贷方

时间:2019-02-07 11:32:12

标签: sql sql-server

我正在尝试使用存储过程进行借记和贷记,并更新帐户余额。

我的SQL代码如下所示

Query OK, 216616 rows affected, 1082 warnings (6.68 sec)
Records: 216616  Deleted: 0  Skipped: 0  Warnings: 1082

现在要运行存储过程,我可以这样做:

ALTER PROCEDURE DebitCr

@AccountNumber1 varchar(50) = null,
@AccountNumber2 varchar(50) = null,
@balance money = null, 
@amount money = null

AS 
BEGIN

SET NOCOUNT ON;

UPDATE account_Balances SET @balance = @balance - @amount WHERE @AccountNumber1 = @AccountNumber1
UPDATE account_Balances SET @balance = @balance + @amount WHERE @AccountNumber2 = @AccountNumber2

END
GO

由于某些原因,它不能有效地更新帐户余额,我究竟弄错了什么?

4 个答案:

答案 0 :(得分:1)

NULL值会引起问题:

UPDATE account_Balances 
       SET balance = COALESCE(@balance, 0) - COALESCE(@amount, 0)
WHERE AccountNumber1 = @AccountNumber1

UPDATE account_Balances 
       SET balance = COALESCE(@balance, 0) + COALESCE(@amount, 0) 
WHERE AccountNumber2 = @AccountNumber2

答案 1 :(得分:1)

我不明白您为什么要将 balance 传递到存储过程中。它存储在表中。我认为金额是唯一需要的东西:

ALTER PROCEDURE DebitCr (
    @AccountNumber1 varchar(50) = null,
    @AccountNumber2 varchar(50) = null,
    @amount money = null

) AS 
BEGIN
    SET NOCOUNT ON;

    UPDATE account_Balances
        SET balance = COALESCE(balance, 0) - COALESCE(@amount, 0)
        WHERE AccountNumber = @AccountNumber1;

    UPDATE account_Balances
        SET balance = COALESCE(balance, 0) + COALESCE(@amount, 0)
        WHERE AccountNumber = @AccountNumber2;
END;
GO

也就是说,您可能想考虑如何使过程“安全”。这里有一些想法:

  • 要么在单个语句中进行更新,要么将更新包装在事务中,因此它们“同时”生效。
  • 在更改任何值之前,请先检查两个帐户是否有效。
  • 在更改任何值之前,请检查第一个帐户的balance是否足够大。

根据实际业务需求,可能还需要进行其他检查。

答案 2 :(得分:1)

好的,谢谢大家的注意。我遵循了@Suraj Kumar和Gordon Linoff所说的,现在扣除了罚款。谢谢大家

借方贷方的SQL源代码如下:

ALTER PROCEDURE DebitCr (
    @AccountNumber1 varchar(50) = null,
    @AccountNumber2 varchar(50) = null,
    @amount money = null

) AS 
BEGIN
    SET NOCOUNT ON;

    UPDATE account_Balances
        SET balance = COALESCE(balance, 0) - COALESCE(@amount, 0)
        WHERE AccountNumber = @AccountNumber1;

    UPDATE account_Balances
        SET balance = COALESCE(balance, 0) + COALESCE(@amount, 0)
        WHERE AccountNumber = @AccountNumber2;
END;
GO

现在我做这样的事情:

EXEC DebitCr
@AccountNumber1 = '0023889098',
@AccountNumber2 = '0023889133',
@amount = 15000

工作正常,再次感谢。

答案 3 :(得分:0)

您需要将参数更改为列名,如下更新语句中所示。

`export path=$PATH:/media/exe_folder/bin`;
print "Enter the path to your input file:";
$base_dir ="/media/exe_folder";
chomp($CEL_dir = <STDIN>);
opendir (DIR, "$CEL_dir") or die "Couldn't open directory $CEL_dir";
$cel_files = "$CEL_dir"."/cel_files.txt";
open(CEL,">$cel_files")|| die "cannot open $file to write";
print CEL "cel_files\n";
for ( grep { /^[\w\d]/ } readdir DIR ){

print CEL "$CEL_dir"."/$_\n";
}close (CEL);

要处理./R.sh home/folder/inputfile.txt home/folder2/output.txt 的值,可以使用UPDATE account_Balances SET balance = ISNULL(@balance, 0) - ISNULL(@amount,0) WHERE AccountNumber1 = @AccountNumber1 UPDATE account_Balances SET balance = ISNULL(@balance, 0) + ISNULL(@amount,0) WHERE AccountNumber2 = @AccountNumber2