我有2个SQL Server语句。SQL Server事务
Delete from hello where id=1
Insert into hello name,age
select name ,age from welcome
如果其中一个失败,则不应删除或插入。
我试图用交易
Begin Tran
Delete from hello where id=1
Insert into hello (name,age)
select name ,age from welcome
Commit Tran
但是,如果其中任何一个出了问题。其他一个是committed.Am我失去了一些东西。
2 delete statements
BEGIN TRY
BEGIN TRAN;
delete from hello where id=1
delete from hello where id=19 // here id=19 doesn't exist
COMMIT TRAN;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0 ROLLBACK;
THROW;
END CATCH;
这里id = 19不存在,所以它应该回滚,但它是删除id = 1。它承诺,而不是rollback.What我应该在此之情况做..
参见[用于SQLSERVER交易基本模板(http://stackoverflow.com/questions/290668/basic-template-for-transactions-in-sqlserver) –
感谢有用信息亚历 – havin