`

触发器 jackey

阅读更多

2009.0713

 

insert 触发器

 

CREATE TRIGGER [change_view_tDBDes_when_insert] ON [dbo].[tDBDes]
FOR INSERT
AS
declare @db varchar(255)
declare @sql1 nvarchar(4000), @sql2 nvarchar(4000), @sql3 nvarchar(4000), @sql4 nvarchar(4000), @sql5 nvarchar(4000), @sql6 nvarchar(4000), @sql7 nvarchar(4000)
declare @name1 varchar(255), @name_table varchar(255)
set @name1 = (select name1 from inserted)
set @db = substring(@name1,7,len(@name1)-6)
if(substring(@name1,7,1)=3) set @name_table='tAJB_UID'
else set @name_table='tYHXX'
set @sql1='create view v1_'+@db+' as '
set @sql2='create view v2_'+@db+' as '
set @sql3='create view v3_'+@db+' as '
set @sql4='create view v4_'+@db+' as '
set @sql5='create view v5_'+@db+' as '
set @sql6='create view v6_'+@db+' as '
set @sql7='create view v7_'+@db+' as '
if(substring(@db,1,1)=1)
begin
set @sql1=@sql1+'(SELECT a.*,UIDS,YHMC,YHDZ,EMAIL,YHZT,comment,JJLJHM,short_address,convert(char(20),CJSJ,120) CJSJ1,convert(char(20),ZHXGSJ,120) ZHXGSJ1,convert(char(20),ZHCSSJ,120) ZHCSSJ1,'''' reserved1,b.handlePlan as uidHandlepLan '
set @sql1=@sql1+' FROM vYH a,'+@name1+'.dbo.'+@name_table+' b'
set @sql1=@sql1+' WHERE a.UID=b.UID)'
end
else if(substring(@db,1,1)=3)
begin
set @sql1=@sql1+'(SELECT a.*,UIDS,YHMC,YHDZ,EMAIL,YHZT,comment,'''' JJLJHM,short_address,convert(char(20),CJSJ,120) CJSJ1,convert(char(20),ZHXGSJ,120) ZHXGSJ1,convert(char(20),ZHCSSJ,120) ZHCSSJ1,c.reserved1,b.handlePlan as uidHandlepLan, d.PlanDescription as uidPlanDescription '
set @sql1=@sql1+' FROM vYH a inner join '+@name1+'.dbo.'+@name_table+' b on a.UID=b.UID left join  CACDB_'+@db+'.dbo.userinfo c'
set @sql1=@sql1+' on substring(b.UID,11,10)=substring(c.id,1,10) left join tPlanManager d on d.PlanID =b.handlePlan)'
end
else
begin
set @sql1=@sql1+'(SELECT a.*,UIDS,YHMC,YHDZ,EMAIL,YHZT,comment,'''' JJLJHM,short_address,convert(char(20),CJSJ,120) CJSJ1,convert(char(20),ZHXGSJ,120) ZHXGSJ1,convert(char(20),ZHCSSJ,120) ZHCSSJ1,'''' reserved1,b.handlePlan as uidHandlepLan,c.PlanDescription as uidPlanDescription'
set @sql1=@sql1+' FROM vYH a inner join '+@name1+'.dbo.'+@name_table+' b'
set @sql1=@sql1+' on a.UID=b.UID left join tPlanManager c on c.PlanID =b.handlePlan)'
end
set @sql2=@sql2+'(select pre_UID,a.address,description,a.handlePlan,d.PlanDescription,c.UID,YHZT,comment'
set @sql2=@sql2+' from tArea a inner join '+@name1+'.dbo.'+@name_table+' c'
set @sql2=@sql2+' on a.pre_UID='''+substring(@name1,7,len(@name1)-6)+''' left join tPlanManager d on d.PlanID=a.handlePlan)'
set @sql3=@sql3+'(select a.YHDZ UID_Address, a.short_address, a.UID'
set @sql3=@sql3+' from '+@name1+'.dbo.'+@name_table+' a)'
set @sql4=@sql4+'(select a.id operator_id,a.loginId loginId,b.*,convert(char(20),AlarmDateTime,120) AlarmDateTime1'
set @sql4=@sql4+' from CACDB_User_Log.dbo.Handle_Alarm_Log_'+@db+' b left join tOperator a '
set @sql4=@sql4+' on b.Handler_ID=a.id)'
set @sql5=@sql5+'(select a.UID,a.monitor_id,a.mainAccount'
set @sql5=@sql5+' from '+@name1+'.dbo.tUser_Monitor a, CACDB_S1000.dbo.vYH b'
set @sql5=@sql5+' where a.isMonitor=1 and a.UID=b.UID and b.ifMonitor=2)'
set @sql5=@sql5+' union all (select a.UID,b.id,0 from '+@name1+'.dbo.'+@name_table+' a, CACDB_S6000.dbo.tMonitor b, t_c6101group_Area c, t_c6101group_monitor d where c.area_id='''+substring(@name1,7,len(@name1)-6)+''' and c.group_id=d.group_id and d.monitor_id=b.id)'
set @sql6=@sql6+'(select a.UID,a.monitor_id,a.mainAccount'
set @sql6=@sql6+' from '+@name1+'.dbo.tUser_Monitor a, CACDB_S1000.dbo.vYH b'
set @sql6=@sql6+' where a.UID=b.UID and b.ifMonitor=2)'
set @sql6=@sql6+' union all (select a.UID,b.id,0 from '+@name1+'.dbo.'+@name_table+' a, CACDB_S6000.dbo.tMonitor b, t_c6101group_Area c, t_c6101group_monitor d where c.area_id='''+substring(@name1,7,len(@name1)-6)+''' and c.group_id=d.group_id and d.monitor_id=b.id)'
--add by locker v7
if(substring(@db,1,1)='5')
begin
set @sql7=@sql7+'(SELECT a.c_hsb_cs_id as smasId,a.c_state,a.c_ser_healthchkintervalsecond,a.c_proxy_ip,a.c_register_ip,c.YHMC,c.YHZT,c.JJLJHM,c.UIDLevel,c.sendInfoToRelatedPerson,c.RelationCamer,d.* '
set @sql7=@sql7+' FROM '+@name1+'.dbo.mst_hsbpara a  INNER JOIN ' + @name1 +'.dbo.'+@name_table+' c ON c.hsb_userid = a.c_hsb_cs_id inner join vYH d on d.UID=c.UID)'
exec(@sql7)
end
exec(@sql1)
exec(@sql2)
exec(@sql3)
exec(@sql4)
exec(@sql5)
exec(@sql6)
declare x cursor for select name1 from tDBDes
open x
set @sql1='alter view vUser as '
set @sql2='alter view vArea as '
set @sql3='alter view v_UID_address as '
set @sql4='alter view vAlarmLog_operator as '
set @sql5='alter view vUID_Monitor as '
set @sql6='alter view vUID_Monitor_All as '
set @sql7='alter view v_SmasIDtoUser as '
fetch next from x into @name1
while(@@FETCH_STATUS=0)
begin
 set @db = substring(@name1,7,len(@name1)-6)
 
 set @sql1=@sql1+'(select * from v1_'+@db+')'
 set @sql1=@sql1+' UNION ALL '
 
 set @sql2=@sql2+'(select * from v2_'+@db+')'
 set @sql2=@sql2+' UNION ALL '
 
 set @sql3=@sql3+'(select * from v3_'+@db+')'
 set @sql3=@sql3+' UNION ALL '
 
 set @sql4=@sql4+'(select * from v4_'+@db+')'
 set @sql4=@sql4+' UNION ALL '
 
 set @sql5=@sql5+'(select * from v5_'+@db+')'
 set @sql5=@sql5+' UNION ALL '
 
 set @sql6=@sql6+'(select * from v6_'+@db+')'
 set @sql6=@sql6+' UNION ALL '
 print @db
 if(substring(@db,1,1)='5') set @sql7=@sql7+'(select * from v7_'+@db+')' + ' UNION ALL '
 
 fetch next from x into @name1
end
close x
deallocate x
set @sql1=substring(@sql1,1,len(@sql1)-10)
set @sql2=substring(@sql2,1,len(@sql2)-10)
set @sql3=substring(@sql3,1,len(@sql3)-10)
set @sql4=substring(@sql4,1,len(@sql4)-10)
set @sql5=substring(@sql5,1,len(@sql5)-10)
set @sql6=substring(@sql6,1,len(@sql6)-10)
set @sql7=substring(@sql7,1,len(@sql7)-10)
print @sql7
exec(@sql1)
exec(@sql2)
exec(@sql3)
exec(@sql4)
exec(@sql5)
exec(@sql6)
exec(@sql7)

 

 

 

 

 

 

 

删除触发器

 

CREATE TRIGGER [change_view_tDBDes_when_delete] ON [dbo].[tDBDes]
FOR DELETE
AS
declare @db varchar(255)
declare @sql1 nvarchar(4000), @sql2 nvarchar(4000), @sql3 nvarchar(4000), @sql4 nvarchar(4000), @sql5 nvarchar(4000), @sql6 nvarchar(4000), @sql7 nvarchar(4000)
declare @name1 varchar(255), @name_table varchar(255)
declare @viewName1 varchar(255),@viewName2 varchar(255),@viewName3 varchar(255),@viewName4 varchar(255),@viewName5 varchar(255),@viewName6 varchar(255),@viewName7 varchar(255)
set @name1 = (select name1 from deleted)
set @db = substring(@name1,7,len(@name1)-6)
if(substring(@name1,7,1)=3) set @name_table='tAJB_UID'
else set @name_table='tYHXX'
set @sql1='drop view v1_'+@db
set @sql2='drop view v2_'+@db
set @sql3='drop view v3_'+@db
set @sql4='drop view v4_'+@db
set @sql5='drop view v5_'+@db
set @sql6='drop view v6_'+@db
set @sql7='drop view v7_'+@db
set @viewName1 = 'v1_'+@db
set @viewName2 = 'v2_'+@db
set @viewName3 = 'v3_'+@db
set @viewName4 = 'v4_'+@db
set @viewName5 = 'v5_'+@db
set @viewName6 = 'v6_'+@db
set @viewName7 = 'v7_'+@db
 if exists (select * from sysobjects where name=''+@viewName1)
 exec(@sql1)
 if exists (select * from sysobjects where name=''+@viewName2)
 exec(@sql2)
 if exists (select * from sysobjects where name=''+@viewName3)
 exec(@sql3)
 if exists (select * from sysobjects where name=''+@viewName4)
 exec(@sql4)
 if exists (select * from sysobjects where name=''+@viewName5)
 exec(@sql5)
 if exists (select * from sysobjects where name=''+@viewName6)
 exec(@sql6)
 if exists (select * from sysobjects where name=''+@viewName7)
 exec(@sql7)
 --print 'aa'
  
declare x cursor for select name1 from tDBDes
open x
set @sql1='alter view vUser as '
set @sql2='alter view vArea as '
set @sql3='alter view v_UID_address as '
set @sql4='alter view vAlarmLog_operator as '
set @sql5='alter view vUID_Monitor as '
set @sql6='alter view vUID_Monitor_All as '
set @sql7='alter view v_SmasIDtoUser as '
fetch next from x into @name1
while(@@FETCH_STATUS=0)
begin
 set @db = substring(@name1,7,len(@name1)-6)
 
 set @sql1=@sql1+'(select * from v1_'+@db+')'
 set @sql1=@sql1+' UNION ALL '
 
 set @sql2=@sql2+'(select * from v2_'+@db+')'
 set @sql2=@sql2+' UNION ALL '
 
 set @sql3=@sql3+'(select * from v3_'+@db+')'
 set @sql3=@sql3+' UNION ALL '
 
 set @sql4=@sql4+'(select * from v4_'+@db+')'
 set @sql4=@sql4+' UNION ALL '
 
 set @sql5=@sql5+'(select * from v5_'+@db+')'
 set @sql5=@sql5+' UNION ALL '
 
 set @sql6=@sql6+'(select * from v6_'+@db+')'
 set @sql6=@sql6+' UNION ALL '
 
 if(substring(@db,1,1)='5') set @sql7=@sql7+'(select * from v7_'+@db+')' + ' UNION ALL '
 
 
 fetch next from x into @name1
end
close x
deallocate x
set @sql1=substring(@sql1,1,len(@sql1)-10)
set @sql2=substring(@sql2,1,len(@sql2)-10)
set @sql3=substring(@sql3,1,len(@sql3)-10)
set @sql4=substring(@sql4,1,len(@sql4)-10)
set @sql5=substring(@sql5,1,len(@sql5)-10)
set @sql6=substring(@sql6,1,len(@sql6)-10)
set @sql7=substring(@sql7,1,len(@sql7)-10)
exec(@sql1)
exec(@sql2)
exec(@sql3)
exec(@sql4)
exec(@sql5)
exec(@sql6)
exec(@sql7)

 

 

分享到:
评论

相关推荐

    随便写的代码-jacky

    根据提供的文件信息,我们可以梳理出以下几个关键的知识点: ### SQL语句:创建表与主键定义 #### 创建表 `dc`: ```sql CREATE TABLE dc ( tid NUMBER(10) NOT NULL, tname VARCHAR(10), taddress VARCHAR(10)...

    [企业政府]Jacky法律在线网站源码_falv.zip源码ASP.NET网站源码打包下载

    [企业政府]Jacky法律在线网站源码_falv.zip源码ASP.NET网站源码打包下载[企业政府]Jacky法律在线网站源码_falv.zip源码ASP.NET网站源码打包下载[企业政府]Jacky法律在线网站源码_falv.zip源码ASP.NET网站源码打包...

    Fetion Tool 最简单的飞信工具 by jacky_luo .rar

    "最简单的飞信工具 by jacky_luo"表明这是一个由用户jacky_luo开发的简化版或优化版飞信工具,可能是为了提高用户体验或者增加某些特定功能。 在描述中,我们看到的信息与标题相同,进一步确认这是一个由jacky_luo...

    信息安全_数据安全_AppSecEU2016-Jacky-Fox-Women-in-Cyber.pdf

    AppSec EU 2016 大会上,Jacky Fox,Deloitte Ireland 的 Cyber & IT Forensic 领导者,就“Women in Cyber Security”发表了演讲,探讨了性别与网络安全的关系以及如何吸引并保留女性人才在这一领域。 首先,Jacky...

    Jacky的打飞机游戏 已在Eclipse能运行,含有APK文件

    Jacky的打飞机游戏 已在Eclipse能运行,含有APK文件 感谢Jacky的教程 和 他的源码,嘿嘿!! 在此附上他的博客地址 ,一起支持他http://blog.csdn.net/jackystudio/article/category/1591201

    Jacky_Dai.rar_解方程_解方程组_高斯消去法

    这个名为"Jacky_Dai.rar_解方程_解方程组_高斯消去法"的压缩包文件似乎包含了使用VC++编程语言实现的高斯消去法程序,它可以帮助我们理解如何通过编程来解决数学中的线性系统问题。 高斯消去法,又称为高斯简化法,...

    内存检测多合一 光盘版

    这个光盘版的内存检测工具包含多个版本,如memtest JACKY版2.1和4.2,以及RST(可能是指RAID Storage Technology)相关的功能。这些工具都是专门设计用来测试内存条的稳定性、兼容性和错误率的。 首先,让我们详细...

    winform_ftp_v2.0_jacky20200126.rar

    本项目“winform_ftp_v2.0_jacky20200126”提供了一个完整的FTP客户端源码,支持文件的上传、下载以及列出文件和目录列表功能,特别适合初学者或开发者作为参考和学习。下面将对该项目的核心知识点进行深入讲解。 ...

    ASP.NET源码——Jacky法律在线网站源码.zip

    这个“Jacky法律在线网站源码”是一个使用ASP.NET技术开发的法律咨询服务网站的完整源代码,旨在帮助开发者理解如何在实际项目中应用ASP.NET进行网页设计和功能实现。 1. ASP.NET框架基础: ASP.NET提供了丰富的...

    ASP.NET源码——[企业]Jacky法律在线网站源码.zip

    这个压缩包"ASP.NET源码——[企业]Jacky法律在线网站源码.zip"包含了构建一个企业级法律在线服务平台的源代码,我们可以从中学习和理解ASP.NET在实际项目中的应用。 首先,ASP.NET的核心组件包括ASP.NET Web Forms...

    jacky_dai的博客文章.pdf

    在Jacky_dai的这篇博客文章中,涵盖了多个C++编程和Windows开发的相关知识点,包括标准模板库(STL)的List、内联函数与宏定义的对比、宏的巧妙使用、Windows Template Library(WTL)的学习资源、报文传输中的进制转换...

    「安全管理」AppSecEU2016-Jacky-Fox-Women-in-Cyber - NGFW.zip

    「安全管理」AppSecEU2016-Jacky-Fox-Women-in-Cyber - NGFW 安全建设 安全方案 数据库审计 系统安全 NGFW

    Eclipse中文教程(Jacky Lee)

    这是一个Eclipse使用的基础篇,适合于刚开始接触Eclipse的朋友们学习。

    shopOfSweets_jacky415:shopOfSweets_jacky415 ---关于糖果店的多彩网站,使用了Bootstrap

    shopOfSweets_jacky415 shopOfSweets_jacky415shopOfSweets_jacky415 ---关于糖果店的多彩网站,使用了Bootstrap

    Jacky说用户体验︱软件UX模型在硬件产品领域的应用.docx

    这篇文章通过Jacky的分享,探讨了如何将软件UX模型应用于硬件产品,以提高其用户体验。 首先,我们要明确用户体验的基本概念。UX不仅关乎产品的功能性,还涉及到用户的情感反应和满意度。传统上,可用性工程(UE)...

    Jacky法律在线网站源码_falv.zip

    源码参考,欢迎下载

    Symantec云安全解决方案-jacky zhu.docx

    【Symantec云安全解决方案】 在当今数字化转型的时代,云计算已经成为了企业信息化的重要支柱,但同时也带来了新的安全挑战。Symantec作为一家知名的网络安全公司,提供了全面的云安全解决方案来帮助企业应对这些...

    jacky415.github.io:jacky415.github.io ---我的个人网站-我的投资组合; 网站重塑计划

    【标题】"jacky415.github.io:jacky415.github.io ---我的个人网站-我的投资组合; 网站重塑计划" 暗示这是一个关于个人网站的项目,其中包含了用户的投资组合展示,并且正在进行网站的更新或改造。这个项目很可能是...

    Linux 控制台终端

    在Linux系统中,控制台终端是操作系统与用户交互的基础,尤其在没有图形界面的情况下,控制台终端显得尤为重要。本文将详细解析Linux控制台参数以及printk如何将信息输出到控制台的过程。 首先,Linux内核在启动时...

    Jacky-Summer#personal-blog#理解 redux-thunk 源码1

    前言前面几篇我们就 Redux 展开了几篇文章,这次我们来实现 react-thunk,就不是叫实现 redux-thunk 了,直接上源码,因为源码就 11

Global site tag (gtag.js) - Google Analytics