ich eine einfache SQL-Anweisung eine Tabelle wie folgt zu erstellen:SQL Server Transaktion erfolgreich falsch Id
Create table tblAccountBalance
(
Id int,
AccountName nvarchar(200),
Balance int
)
insert into tblAccountBalance values (1, 'Mark', 1000);
insert into tblAccountBalance values (2, 'Mary', 1000);
in Resultierende
Id AccountName Balance
-----------------------
1 Mark 1000
2 Mary 1000
Dann erstelle ich eine Transaktion wie folgt aus:
begin try
begin transaction -- must have transaction keyword here!
update tblAccountBalance
set Balance = Balance - 100
where Id = 1
update tblAccountBalance
set Balance = Balance + 100
where Id = 2
commit transaction --or simply commit, but by default, it is commit WORK - thus you cannot specify transaction name, best is to put transaction
print 'Transaction successful!'
end try
begin catch
rollback transaction --or simply rollback, but again, it might be similar to transaction, best is to put transaction keyword
print 'Transaction is rolled back!'
end catch
Die, wenn ausgeführt, überträgt einfach 100 Einheiten Geld von Mark zu Mary:
Id AccountName Balance
-----------------------
1 Mark 900
2 Mary 1100
nun aus der oben fort - aktualisiert - Tabelle, Fehler in der Transaktion einzuführen, ändere ich die Transaktion, um die zweite Update-Anweisung nvarchar
als Id wie dies mit zu machen:
begin try
begin transaction
update tblAccountBalance
set Balance = Balance - 100
where Id = 1
update tblAccountBalance
set Balance = Balance + 100
where Id = '24' -- note the Id here is changed
commit transaction
print 'Transaction successful!'
end try
begin catch
rollback transaction
print 'Transaction is rolled back!'
end catch
Zu meinem Überraschung, die obige Abfrage führt zu:
(1 row(s) affected)
(0 row(s) affected)
Transaction successful!
Id AccountName Balance
-----------------------
1 Mark 800
2 Mary 1100
Welches ist nicht das gewünschte Ergebnis.
Aber dann, wenn ich von der unerwünschten aktualisierte Tabelle Ergebnis oben mit einer modifizierten Transaktion (wiederum in der zweiten Update Id) so weiter weiter:
begin try
begin transaction
update tblAccountBalance
set Balance = Balance - 100
where Id = 1
update tblAccountBalance
set Balance = Balance + 100
where Id = 'A24' -- note the Id here is changed
commit transaction
print 'Transaction successful!'
end try
begin catch
rollback transaction
print 'Transaction is rolled back!'
end catch
Daraus ergibt sich:
(1 row(s) affected)
(0 row(s) affected)
Transaction is rolled back!
Id AccountName Balance
------------------------
1 Mark 800
2 Mary 1100
Welches ist mein gewünschtes Ergebnis. Was stimmt nicht mit der zweiten Transaktion? Warum wird das Update noch ausgeführt?
Ich verwende SQL Server 2014, wenn das von Bedeutung ist.
Oh mein .. Ich habe es falsch wegen der impliziten Konvertierung. Danke für Ihre Erklärung. – Ian
Danke für Ihre Antwort. Es ist identisch mit anderen, aber Sie sind der erste, der antwortet und einen Verweis auf die Priorität des MSDN-Datentyps hat. – Ian