SQL SERVER存储过实现转账

    xiaoxiao2022-07-03  127

    CREATE PROC sp_Transfer ( @monney int ) AS BEGIN BEGIN TRAN Tran_Money --开始事务 DECLARE @tran_error int; SET @tran_error = 0; BEGIN TRY UPDATE bank SET MyMoney = MyMoney - @monney WHERE Name = 'admin'; SET @tran_error = @tran_error + @@ERROR; --测试admin的钱减少,admin1钱是否会增加 --SET @tran_error = 1; UPDATE bank SET MyMoney = MyMoney + @monney WHERE Name = 'admin1; SET @tran_error = @tran_error + @@ERROR; END TRY BEGIN CATCH PRINT '出现异常,错误编号:' + convert(varchar,error_number()) + ',错误消息:' + error_message() SET @tran_error = @tran_error + 1 END CATCH IF(@tran_error > 0) BEGIN --执行出错,回滚事务 ROLLBACK TRAN; PRINT '转账失败,取消交易!'; END ELSE BEGIN --没有异常,提交事务 COMMIT TRAN; PRINT '转账成功!'; END END go EXEC sp_Transfer '30';

     

    最新回复(0)