事务

Begin Transaction 事务开始

Commit Transaction 事务结束

Rollback [Tran[saction] 事务回滚

[transaction_name|@tran_name_variable|savepoint_name|@savepoint_variable]]

如果要让事务回滚到指定位置,则需要在事务中设定保存点Save Point

 

例子:

begin transaction my_transaction_delete

use sample

go 

delete from department where dept_id='1012'

save transaction after_delete

update employee set dept_id='1001' where dept_id='1012'

if @@error!=0 or @@rowcount=0 then 

begin

 rollback tran after_delete /*如果使用rollback my_transaction_delete则会回滚到事务开始前*/

print'更新员工信息表时产生错误'

commit transaction my_transaction_delete

go

 

 

 

事务:

Create Procedure   DeleteUser

(

@UserID int

)

AS

begin transaction

sql语句1

sql语句2

if @@error<>0

rollback transaction

else

commit transaction

return

你可能感兴趣的:(事务)