[SQL]开启事物,当两条插入语句有出现错误的时候,没有错误的就插入到表中,错误的语句不影响到正确的插入语句

时间:2023-08-25 15:49:14
begin transaction mustt
insert into student values(,'kkk','j大洒扫','j','djhdjh') insert into student values(,'jhsjhs','j','h','asjkdjk')
if @@ERROR<>
BEGIN
ROLLBACK TRANSACTION mustt
print 'error'
RETURN
END
COMMIT TRANSACTION mustt //在上面的事务中,当两条插入语句有出现错误的时候,没有错误的就插入到表中,错误的语句不影响到正确的插入语句。

-----------------------------------------------------------------------------------------------------------

保存事物

保存事务:
begin transaction savemustt
insert into student values(,'kkk','j','j','djhdjh')
save transaction before_insert_data2
insert into student values(,'jhsjhs','j','h','asjkdjk') ROLLBACK TRANSACTION before_insert_data2 /*这样就以第一条记录为保存点,且只插入第一条记录,*/