博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sqlserver trigger
阅读量:4639 次
发布时间:2019-06-09

本文共 1746 字,大约阅读时间需要 5 分钟。

1 --==================================== 2 --  Create database trigger template  3 --==================================== 4 USE [EasyJobExTest] 5 GO 6  7 --判断触发器是否存在,存在则删除 8 IF EXISTS( 9   select top 1 t.name as trigger_name,a.name as table_name from sys.triggers t,sys.objects a 10 where Lower(a.name)='section' and t.parent_id=a.object_id and Lower(t.name)='tr_section_insert'11 )12 begin13     DROP TRIGGER tr_section_insert;14 end15 GO16 17 /*before触发器instead of*/18 CREATE TRIGGER tr_section_insert ON section 19     instead of insert,delete,update20     --for delete --after触发器delete21     --for insert --after触发器insert22     --for update --after触发器update23 AS 24 begin25 declare @SecId    int;26 declare @SecName    nvarchar(50);27 declare @Remark    nvarchar(200);28 declare @Department    int;29 declare @CreateTime    datetime;30 declare @Deepness    int;31 declare @ManageUserID    int;32 33 select @SecId=secid,@SecName=t.SecName,@Remark=t.Remark,@Department=t.Department,@CreateTime=t.CreateTime,34 @Deepness=t.Deepness,@ManageUserID=t.ManageUserID from inserted t;35 36 if exists(select top 1 * from inserted)37 begin38 print 'trigger insert'39     IF @CreateTime is NULL40     begin41         select @CreateTime = Sysdatetime()42         print '当前时间:'43         print @CreateTime44     end45         insert into Section values(@SecId,@SecName,@Remark,@Department,@CreateTime,@Deepness,@ManageUserID);46 end47 else if exists(select top 1 * from deleted)48 begin49 print 'trigger delete'50 end51 else52 print 'trigger update'53 54 /*55 IF IS_MEMBER ('db_owner') = 056 BEGIN57    PRINT 'You must ask your DBA to drop or alter tables!' 58    ROLLBACK TRANSACTION59 END60 */61 end62 go

 

转载于:https://www.cnblogs.com/consuvi/p/4874614.html

你可能感兴趣的文章
马尔可夫方程的解
查看>>
#敏捷个人# 第二批敏捷个人推广者实践团报名
查看>>
敏捷开发本质 与 敏捷个人本质
查看>>
.vimrc
查看>>
Coding源码学习第一部分(AppDelegate.m)
查看>>
VS使用过程中的一些问题
查看>>
极限编程在WEB开发中的作用
查看>>
printf的使用
查看>>
NLP Attention
查看>>
PHP 之数据类型判断
查看>>
第二次冲刺 站立会议3
查看>>
LA3029最大子矩阵
查看>>
万网域名MX解析设置方案[net.cn, ubuntu]
查看>>
403. Frog Jump
查看>>
C++学习之二分查找续
查看>>
Vue创建SPA那些事
查看>>
python基础学习1-列表推导式和字典推导式
查看>>
Linux下开发python django程序(模板设置和载入数据)
查看>>
mfc Radio Buttons
查看>>
Python【第三章】:python 面向对象 (new)
查看>>