SQL Server 2005 nýlega intoduced leið til Rollback viðskiptum í SQL Server 2005
SET XACT_ABORT ON
þegar XACT_ABORT er á. The T-SQL staðhæfing sjálfkrafa rollback viðskiptin þegar villa kom upp í yfirlýsingum.
SET XACT_ABORT ON
NOTKUN Emp
Byrjaðu Tran
Settu inn EmpInfo Values(1)
Settu inn EmpInfo Values(1) — Þessi yfirlýsing mun hækka aðal lykill villa
Settu inn EmpInfo Values(2)
Fremja Tran
Fyrsta lína yfirlýsing mun framkvæma án villur. Helsti lykill villa mun hækka í annarri línu. En allt blokk Kaupin munu ekki vera nefnd vegna þess að XACT_ABORT í á.
You should note that XACT_ABORT terminates the current *batch*, not the entire command. Batches are separated with the GO keyword. Execution will continue at the beginning of the next batch.
This feature is also in SQL Server 2000, though it behaves slightly differently for various errors.
In SQL 2000, there are cases where XACT_ABORT fails to rollback the transaction automatically.
For instance:
Inserting into an invalid column name.
Inserting into a primary key with IDENTITY_INSERT OFF.
Executing a SELECT that uses a non-existent UDF.
These types of errors are usually due to a poorly authored command, so they don’t occur in the day-to-day operation of your program unless someone alters the schema. But I account for their possibility when authoring database update scripts when deploying a new version of a project, just in case deployment doesn’t go as planned.
There are ways to handle these errors. Your COMMIT should be in the batch performing the DML statements. After the batch (after the GO keyword) you can check the @@TRANCOUNT to see if it’s greater than zero and act accordingly. Usually ROLLBACK the transaction.
Transactions can span batches, so you can execute a rollback in a subsequent batch.