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

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

SQL Server重溫 事務(wù)

2024-08-31 00:44:53
字體:
供稿:網(wǎng)友
為什么使用事務(wù)
  當(dāng)對多個表進(jìn)行更新的時候,某條執(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ù)時,如果嵌套內(nèi)的事務(wù)已經(jīng)回滾過則會有異常。此時需要使用事務(wù)保存點(diǎn)。如上代碼。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 南靖县| 浦城县| 郧西县| 盘山县| 武宁县| 崇文区| 南安市| 丰县| 三穗县| 郑州市| 化德县| 贵阳市| 泰兴市| 宣恩县| 梅河口市| 杭锦旗| 泊头市| 桓仁| 磐石市| 会昌县| 轮台县| 鄂州市| 章丘市| 宜宾县| 澳门| 镇远县| 钟祥市| 吉安县| 进贤县| 南部县| 平湖市| 新丰县| 民乐县| 甘泉县| 临武县| 江川县| 肇州县| 商水县| 客服| 余庆县| 山丹县|