我在尝试执行t-sql存储过程时遇到标题错误
EXEC Add500ToChecking 18568
我尝试删除return和go语句,但它没有工作
USE [JProCo]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[Add500ToChecking] @CustID INT AS
BEGIN TRAN
UPDATE SavAccount SET Balance = Balance - 500
WHERE CustomerID = @CustID
IF ( (SELECT Balance FROM SavAccount WHERE CustomerID = @CustID) < 0)
ROLLBACK TRAN
RETURN
UPDATE CkAccount SET Balance = Balance + 500
WHERE CustomerID = @CustID
COMMIT TRAN
RETURN
GO
知道我的代码会发生什么变化吗?这是一个课程项目
答案 0 :(得分:3)
您的BEGIN
声明需要一对END
和IF
:
UPDATE SavAccount SET Balance = Balance - 500
WHERE CustomerID = @CustID
IF ( (SELECT Balance FROM SavAccount WHERE CustomerID = @CustID) < 0)
BEGIN
ROLLBACK TRAN
RETURN
END
如上所述,只有ROLLBACK TRAN
是IF
语句的一部分,因此如果IF
检查失败,它不会回滚事务,而是立即执行{{1} }}