msdn 把這transaction 和錯誤處理講得很不錯。
有什麼不清楚可以看看那個。
這裡摘抄重要的幾句話。
"
If an error prevents the successful completion of a transaction, SQL Server automatically rolls back the transaction and frees all resources held by the transaction.
If a run-time statement error (such as a constraint violation) occurs in a batch, the default behavior in the Database Engine is to roll back only the statement that generated the error.
It is important to keep transactions as short as possible. When a transaction is started, a database management system (DBMS) must hold many resources until the end of the transaction to protect the atomicity, consistency, isolation, and durability (ACID) properties of the transaction.
A TRY…CATCH construct catches all execution errors that have a severity higher than 10 that do not close the database connection.
Errors trapped by a CATCH block are not returned to the calling application."
(如果想返回錯誤給程式或者用戶端,使用RAISERROR)