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

首頁 > 數據庫 > SQL Server > 正文

SQL Server觸發器和事務用法示例

2020-07-25 12:45:02
字體:
來源:轉載
供稿:網友

本文實例講述了SQL Server觸發器和事務用法。分享給大家供大家參考,具體如下:

新增和刪除觸發器

alter trigger tri_TC on t_c  for INSERT,deleteasbegin  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);  endend

更新觸發器和事務

事務主要用在數據的保護,在多表更新時,事務保存所有事務下的更新語句就不會提交,數據也就不能更新成功

alter trigger tri_TC_Update on t_c  for updateasbegin  declare @delcount int;  set @delcount = (select count(*) from deleted);  if(@delcount > 0)  begin   begin transaction triUpdate --定義事務   declare @cname varchar(100);   select @cname = cname from inserted; --保存更新后的內容   update t_c2 set cname = @cname where cid = (select cid from deleted); --更新   if (@@error <> 0)   begin    rollback transaction triUpdate; --事務回滾   end   else   begin    commit transaction triUpdate;  --事務提交   end  endend

存儲過程

if(exists(select name from sysobjects s where s.name='pro_fun' and s.type='p'))  drop procedure pro_fungo  create procedure pro_funas  select * from tablegoexec pro_fun

游標

declare @qybh varchar(10)declare cur cursor for  select distinct qybh from PJ_EnterpriseInputopen curfetch next from cur into @qybhwhile @@fetch_status = 0 begin  print(@qybh)  fetch next from cur into @qybh endclose curdeallocate cur

視圖

alter view CreateViewas select qybh from CreateViewgo

定義方法

alter function funName(@str1 varchar(10),@str2 varchar(10))returns varchar(10)asbegin  declare @returnStr varchar(10)  set @returnStr = 'false'  if(@str1 > @str2)    set @returnStr = 'true'  return @returnStrendselect dbo.funName(... , ...)

定義表變量

declare @qybhTable table (id varchar(32),qybh varchar(30))insert into @qybhTableselect id,qybh from PJ_EnterpriseInputselect * from @qybhTable

case when then 條件統計時的使用

selectsum(case when z.watchName='注冊監理工程師' then 1 else 0 end),sum(case when z.watchName='xinza' then 1 else 0 end),sum(case when z.watchName='監理員' then 1 else 0 end)from zu_corjl zright join zu_corjltemp t on t.corID=z.corID

希望本文所述對大家SQL Server數據庫程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 米脂县| 邵东县| 河曲县| 普格县| 洞头县| 台安县| 班玛县| 精河县| 镇康县| 家居| 莱芜市| 洛川县| 准格尔旗| 万宁市| 伊通| 黔南| 沽源县| 红原县| 五原县| 大冶市| 莱芜市| 扎兰屯市| 长葛市| 科尔| 磐安县| 湟源县| 张家口市| 青阳县| 临高县| 宝鸡市| 堆龙德庆县| 北票市| 长丰县| 衡阳市| 息烽县| 丰顺县| 平陆县| 扶沟县| 民丰县| 密山市| 顺昌县|