Hi! Could we please enable some services and cookies to improve your experience and our website?

SQLize | PHPize | SQLtest

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

A A A
Login    Share code      Blog   FAQ
Copy Format Clear
CREATE PROCEDURE sp_ProcessPayment @OrderID INT, @Amount DECIMAL(10,2), @PaymentMethod VARCHAR(20) AS BEGIN BEGIN TRY BEGIN TRANSACTION; -- 开始事务 -- 1. 更新订单状态 UPDATE Orders SET Status = 'Paid', PaymentDate = GETDATE() WHERE OrderID = @OrderID; -- 2. 记录支付信息 INSERT INTO Payments(OrderID, Amount, PaymentMethod, PaymentDate) VALUES(@OrderID, @Amount, @PaymentMethod, GETDATE()); -- 3. 更新账户余额(如果有会员系统) UPDATE Customers SET Balance = Balance - @Amount WHERE CustomerID = (SELECT CustomerID FROM Orders WHERE OrderID = @OrderID); COMMIT TRANSACTION; -- 提交事务 PRINT '支付处理成功'; END TRY BEGIN CATCH IF @@TRANCOUNT > 0 -- 检查是否有活跃事务 ROLLBACK TRANSACTION; -- 回滚事务 PRINT '支付处理失败:' + ERROR_MESSAGE(); END CATCH END

Stuck with a problem? Got Error? Ask AI support!

Copy Clear