Sql server 删除查询在SQL Server中不起作用
这很奇怪,但我的SQL Server不删除项目: 我有这样一个存储过程:Sql server 删除查询在SQL Server中不起作用,sql-server,Sql Server,这很奇怪,但我的SQL Server不删除项目: 我有这样一个存储过程: Create Procedure DeleteItem @cnm nchar(10) as Begin Transaction Delete From T1 where Cnm = @cnm Delete From T2 where Cnm = @cnm if @@ERROR <> 0 Begin Commit
Create Procedure DeleteItem @cnm nchar(10)
as
Begin Transaction
Delete From T1 where Cnm = @cnm
Delete From T2 where Cnm = @cnm
if @@ERROR <> 0
Begin
Commit
End
Else
Begin
RollBack
End
它在消息
窗格中返回:
(1 row(s) affected)
(1 row(s) affected)
但当我从这些表中获取数据时,删除的记录仍然存在
我做错了什么吗?如果前面的Transact-SQL语句没有遇到错误,则返回0
更改您的<代码>如果条件
if @@ERROR = 0
Commit
else
....
您可能应该检查两次删除的错误,而不仅仅是第二次。您应该使用
try…catch
。不检查@@error
。
if @@ERROR = 0
Commit
else
....