-- 创建触发器,实现转账时账户余额的自动更新 CREATE TRIGGER UpdateAccountBalance ON TransferRecord AFTER INSERT AS BEGIN UPDATE Account SET AccountBalance = AccountBalance - i.TransferAmount FROM Account a JOIN inserted i ON a.AccountID = i.FromAccountID; UPDATE Account SET AccountBalance = AccountBalance + i.TransferAmount FROM Account a JOIN inserted i ON a.AccountID = i.ToAccountID; END; -- 创建存储过程,实现存款和取款操作时账户余额的自动更新
时间: 2023-07-16 19:12:47 浏览: 146
体育课评分系统 微信小程序+SSM毕业设计 源码+数据库+论文+启动教程.zip
CREATE PROCEDURE UpdateAccountBalance
@AccountID int,
@TransactionType varchar(10),
@TransactionAmount decimal(18,2)
AS
BEGIN
IF @TransactionType = 'Deposit'
BEGIN
UPDATE Account
SET AccountBalance = AccountBalance + @TransactionAmount
WHERE AccountID = @AccountID
END
ELSE IF @TransactionType = 'Withdraw'
BEGIN
UPDATE Account
SET AccountBalance = AccountBalance - @TransactionAmount
WHERE AccountID = @AccountID
END
END
这个存储过程可以实现存款和取款操作时账户余额的自动更新。在存款时,账户余额会增加存款金额;在取款时,账户余额会减少取款金额。
阅读全文