我有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