国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > SQL Server > 正文

SQL Server重溫 事務(wù)

2020-07-25 13:14:02
字體:
供稿:網(wǎng)友
為什么使用事務(wù)
  當(dāng)對多個(gè)表進(jìn)行更新的時(shí)候,某條執(zhí)行失敗。為了保持?jǐn)?shù)據(jù)的完整性,需要使用事務(wù)回滾。
顯示設(shè)置事務(wù)
復(fù)制代碼 代碼如下:

begin try
begin transaction
insert into shiwu (asd) values ('aasdasda');
commit transaction
end try
begin catch
select ERROR_NUMBER() as errornumber
rollback transaction
end catch

隱式設(shè)置事務(wù)
復(fù)制代碼 代碼如下:

set implicit_transactions on; -- 啟動隱式事務(wù)
go
begin try
insert into shiwu (asd) values ('aasdasda');
insert into shiwu (asd) values ('aasdasda');
commit transaction;
end try
begin catch
select ERROR_NUMBER() as errornumber
rollback transaction; --回滾事務(wù)
end catch
set implicit_transactions off; --關(guān)閉隱式事務(wù)
go

顯示事務(wù)以下語句不能使用,隱式事務(wù)可以
復(fù)制代碼 代碼如下:

alter database;
backup;
create database;
drop database;
reconfigure;
restore;
update statistics;

顯示事務(wù)可以嵌套使用
復(fù)制代碼 代碼如下:

--創(chuàng)建存儲過程
create procedure qiantaoProc
@asd nchar(10)
as
begin
begin try
begin transaction innerTrans
save transaction savepoint --創(chuàng)建事務(wù)保存點(diǎn)
insert into shiwu (asd) values (@asd);
commit transaction innerTrans
end try
begin catch
rollback transaction savepoint --回滾到保存點(diǎn)
commit transaction innerTrans
end catch
end
go
begin transaction outrans
exec qiantaoProc 'asdasd';
rollback transaction outrans

事務(wù)嵌套,回滾外層事務(wù)時(shí),如果嵌套內(nèi)的事務(wù)已經(jīng)回滾過則會有異常。此時(shí)需要使用事務(wù)保存點(diǎn)。如上代碼。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 六盘水市| 衡南县| 衡南县| 平安县| 石门县| 南充市| 商城县| 玉林市| 武胜县| 鹤岗市| 富宁县| 贵德县| 沧源| 巴彦淖尔市| 巫溪县| 平舆县| 贺兰县| 铁岭县| 琼海市| 临朐县| 衡东县| 涡阳县| 西昌市| 益阳市| 河西区| 舒城县| 余姚市| 仙居县| 桂东县| 许昌县| 天等县| 鸡东县| 阜新| 彰武县| 通渭县| 扶余县| 车险| 柳河县| 保亭| 蒲江县| 仁化县|