SQL Server觸發(fā)器和事務(wù)用法示例
本文實(shí)例講述了SQL Server觸發(fā)器和事務(wù)用法。分享給大家供大家參考,具體如下:
新增和刪除觸發(fā)器
alter trigger tri_TC on t_c for INSERT,delete as begin set XACT_ABORT ON declare @INSERTCOUNT int; declare @DELETECOUNT int; declare @UPDATECOUNT int; set @INSERTCOUNT = (select COUNT(*) from inserted); set @DELETECOUNT = (select COUNT(*) from deleted); set @UPDATECOUNT = () if(@INSERTCOUNT > 0) begin insert into t_c2 select * from inserted; end else if(@DELETECOUNT > 0) begin delete t_c2 where exists(select temp.cid from deleted temp where temp.cid=t_c2.cid); end end
更新觸發(fā)器和事務(wù)
事務(wù)主要用在數(shù)據(jù)的保護(hù),在多表更新時(shí),事務(wù)保存所有事務(wù)下的更新語句就不會(huì)提交,數(shù)據(jù)也就不能更新成功
alter trigger tri_TC_Update on t_c
for update
as
begin
declare @delcount int;
set @delcount = (select count(*) from deleted);
if(@delcount > 0)
begin
begin transaction triUpdate --定義事務(wù)
declare @cname varchar(100);
select @cname = cname from inserted; --保存更新后的內(nèi)容
update t_c2 set cname = @cname where cid = (select cid from deleted); --更新
if (@@error <> 0)
begin
rollback transaction triUpdate; --事務(wù)回滾
end
else
begin
commit transaction triUpdate; --事務(wù)提交
end
end
end
存儲(chǔ)過程
if(exists(select name from sysobjects s where s.name='pro_fun' and s.type='p')) drop procedure pro_fun go create procedure pro_fun as select * from table go exec pro_fun
游標(biāo)
declare @qybh varchar(10) declare cur cursor for select distinct qybh from PJ_EnterpriseInput open cur fetch next from cur into @qybh while @@fetch_status = 0 begin print(@qybh) fetch next from cur into @qybh end close cur deallocate cur
視圖
alter view CreateView as select qybh from CreateView go
定義方法
alter function funName(@str1 varchar(10),@str2 varchar(10))
returns varchar(10)
as
begin
declare @returnStr varchar(10)
set @returnStr = 'false'
if(@str1 > @str2)
set @returnStr = 'true'
return @returnStr
end
select dbo.funName(... , ...)
定義表變量
declare @qybhTable table (id varchar(32),qybh varchar(30)) insert into @qybhTable select id,qybh from PJ_EnterpriseInput select * from @qybhTable
case when then 條件統(tǒng)計(jì)時(shí)的使用
select sum(case when z.watchName='注冊監(jiān)理工程師' then 1 else 0 end), sum(case when z.watchName='xinza' then 1 else 0 end), sum(case when z.watchName='監(jiān)理員' then 1 else 0 end) from zu_corjl z right join zu_corjltemp t on t.corID=z.corID
希望本文所述對(duì)大家SQL Server數(shù)據(jù)庫程序設(shè)計(jì)有所幫助。
- MySQL中觸發(fā)器的基礎(chǔ)學(xué)習(xí)教程
- 數(shù)據(jù)庫觸發(fā)器DB2和SqlServer有哪些區(qū)別
- 深入淺析SQL Server 觸發(fā)器
- SqlServer觸發(fā)器詳解
- MYSQL設(shè)置觸發(fā)器權(quán)限問題的解決方法
- mysql觸發(fā)器(Trigger)簡明總結(jié)和使用實(shí)例
- sqlserver數(shù)據(jù)庫使用存儲(chǔ)過程和dbmail實(shí)現(xiàn)定時(shí)發(fā)送郵件
- 使用sqlserver存儲(chǔ)過程sp_send_dbmail發(fā)送郵件配置方法(圖文)
- sqlserver2008自動(dòng)發(fā)送郵件
- 通過sql存儲(chǔ)過程發(fā)送郵件的方法
- SQL server 表數(shù)據(jù)改變觸發(fā)發(fā)送郵件的方法
相關(guān)文章
win2008 r2 安裝sql server 2005/2008 無法連接服務(wù)器解決方法
在與 SQL Server 建立連接時(shí)出現(xiàn)與網(wǎng)絡(luò)相關(guān)的或特定于實(shí)例的錯(cuò)誤。未找到或無法訪問服務(wù)器。請(qǐng)驗(yàn)證實(shí)例名稱是否正確并且 SQL Server 已配置為允許遠(yuǎn)程連接2015-01-01
關(guān)于SQL中CTE(公用表表達(dá)式)(Common Table Expression)的總結(jié)
WITH AS短語,也叫做子查詢部分(subquery factoring),可以讓你做很多事情,定義一個(gè)SQL片斷,該SQL片斷會(huì)被整個(gè)SQL語句所用到2012-08-08
SQLServer 2000 數(shù)據(jù)庫同步詳細(xì)步驟[兩臺(tái)服務(wù)器]
成功實(shí)現(xiàn)SQL Server 2000 數(shù)據(jù)庫同步[一臺(tái)服務(wù)器,一臺(tái)動(dòng)態(tài)IP的備份機(jī)],詳細(xì)步驟說明。2010-07-07
在sqlserver數(shù)據(jù)庫中導(dǎo)入Excel數(shù)據(jù)的全過程
在SQL Server中導(dǎo)入Excel數(shù)據(jù)可以通過使用導(dǎo)入/導(dǎo)出向?qū)硗瓿?下面這篇文章主要給大家介紹了關(guān)于在sqlserver數(shù)據(jù)庫中導(dǎo)入Excel數(shù)據(jù)的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
SqlServer應(yīng)用之sys.dm_os_waiting_tasks 引發(fā)的疑問(下)
這篇文章主要介紹了SqlServer應(yīng)用之sys.dm_os_waiting_tasks 引發(fā)的疑問(下) 的相關(guān)資料,需要的朋友可以參考下2015-12-12
必須會(huì)的SQL語句(四) 數(shù)據(jù)刪除和更新
這篇文章主要介紹了sqlserver中數(shù)據(jù)刪除和更新的sql語句,需要的朋友可以參考下2015-01-01

