Use master
drop TABLE Customer
CREATE TABLE Customer (
ID int PRIMARY KEY,
Account decimal Check (account>0),
Name varchar (20)
)
--use Master
ALTER TABLE Dbo.customer
Add Check (customer.account>0)
SELECT * FROM Customer
INSERT into customer values (2012090101,2000, ' Zhang San ');
INSERT into customer values (2012090102,1000, ' John Doe ');
INSERT into customer values (2012090103,1000, ' Harry ');
BEGIN TRANSACTION
DECLARE @sum int
Set @sum =0
Update customer set account-=200 where customer.id=2012090101
Set @sum +[email protected] @ERROR
Update customer set account+=200 where customer.id=2012090102
Set @sum +[email protected] @ERROR
--update customer set account-=100 where customer.id=2012090103
If @sum <>0
Begin
print ' Transfer failed '
ROLLBACK TRANSACTION
End
Else
Begin
print ' Successful transfer '
Commit TRANSACTION
End
Go
SELECT * FROM Customer
Transaction simulation for SQL Server 2008 implementation