创建表:

CREATE TABLE [dbo].[GeneralRule](
[ID] [int] NOT NULL,
[GeneralRuleName] [nvarchar](50) NULL,
[DeleteFlag] [int] NOT NULL
) CREATE TABLE [dbo].[DetailRule](
[ID] [int] NOT NULL,
[DetailRuleName] [nvarchar](50) NULL,
[ParentId] [int] NULL,
[DeleteFlag] [int] NOT NULL,
) CREATE TABLE [dbo].[DetailRule_bak](
[ID] [int] NOT NULL,
[DetailRuleName] [nvarchar](50) NULL,
[ParentId] [int] NULL,
[DeleteFlag] [int] NOT NULL,
)

创建触发器:

--增加
create trigger triAddGeneralRule
on DetailRule
for insert --为什么事件触发
as
begin
insert into [DetailRule_bak](ID,detailRuleName,ParentId,DeleteFlag)
select ID,detailRuleName,ParentId,DeleteFlag
from INSERTED
end --删除
create trigger triDelGeneralRule
on GeneralRule
for delete --为什么事件触发
as
delete DetailRule
from DetailRule dr,Deleted d
where dr.parentId=d.ID --修改
create trigger triGeneralRule
on GeneralRule
for update --为什么事件触发
as
if update (ID)
begin
update DetailRule
set parentId=i.ID
from DetailRule dr,Deleted d,Inserted i --2个临时表Deleted和Inserted,分别表示触发事件的旧与新记录
where dr.parentId=d.ID
end

if else 触发器

create table employee(emp_id int,emp_name nvarchar(50),gender int,department int,salary numeric(10,2))

create table updated(emp_id int,salary numeric(10,2))

insert into employee values(1,'tom',10,10,6000.00)

insert into updated values(1,6500.00)

create trigger up_salary on employee INSTEAD OF update
as if update (salary)
begin
declare @newSalary numeric(10,2)
declare @oldSalary numeric(10,2)
select @newSalary = salary from updated
select @oldSalary = salary from employee where emp_id = (select emp_id from updated)
if @newSalary > @oldSalary * 1.1
print '工资变动不能超过原来工资的10%'
else
update employee set salary = @newSalary where emp_id = (select emp_id from updated)
end
go

Sq lServer触发器的使用的更多相关文章

  1. 擦亮自己的眼睛去看SQLServer之简单Select(转)

    摘要:这篇文章主要和大家讨论几乎所有人都熟悉,但不少人又陌生的一条select语句. 这篇文章主要和大家讨论几乎所有人都熟悉,但不少人又陌生的一条select语句.不知道大家有没有想过到底是什么东西让 ...

  2. The Windows account sa does not exist and cannot be provisioned as a SQL Server system administrator

    今天遇到一个案例,在使用命令修改一个测试服务器(SQL Server 2014标准版)的服务器排序规则时,遇到了下面错误信息 (具体账号信息脱敏处理,随机生成一个账号密码) The Windows a ...

  3. SQL Server 2012 无人值守安装(加入新实例)

    方法1,通过指定条个參数安装 setup.exe /Q /IACCEPTSQLSERVERLICENSETERMS /ACTION=install /PID=<validpid> /FEA ...

  4. SQL Server 2012 无人值守安装

    方法1,通过指定条个參数安装   setup.exe /Q /IACCEPTSQLSERVERLICENSETERMS /ACTION=install /PID=<validpid> /F ...

  5. Sq server 关于存储过程,触发器的一些理论简述

    http://www.doc88.com/p-2905916227462.html      

  6. Hibernate映射文件创建和删除触发器,存储过程等数据库对象

    创建表,触发器,和存储过程等数据库对象,这里一定要用create 在hibernate.cfg.xml文件中

  7. SQL 视图 局部变量 全局变量 条件语句 事务 触发器

    一.视图 1.视图是一张虚拟表,他所存储的不是实际数据,而是查询语句,但我们可以对视图进行像数据表一样的操作. 2.为什么使用视图呢?我的理解是:1.在远程传输数据时,可以避免过长的查询字符,减少流量 ...

  8. pt-online-schema-change中update触发器的bug

    pt-online-schema-change在对表进行表结构变更时,会创建三个触发器. 如下文测试案例中的t2表,表结构如下: mysql> show create table t2\G . ...

  9. MySQL主从环境下存储过程,函数,触发器,事件的复制情况

    下面,主要是验证在MySQL主从复制环境下,存储过程,函数,触发器,事件的复制情况,这些确实会让人混淆. 首先,创建一张测试表 mysql),age int); Query OK, rows affe ...

随机推荐

  1. java 的三种代理

    java的三种代理模式   1.代理模式 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象访问目标对象.这样做的好处是:可以在目标对象实现的基础上,增强额外的功能操作, ...

  2. IntelliJ IDEA 2017 配置Tomcat 运行Web项目

    以前都用MyEclipse写程序的 突然用了IDEA各种不习惯的说 借鉴了很多网上好的配置办法,感谢各位大神~ 前期准备 IDEA.JDK.Tomcat请先在自己电脑上装好 好么~ 博客图片为主 请多 ...

  3. 生成表结构数据库文档sql语句

    CREATE PROCEDURE [dbo].[生成表结构数据库文档]ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from ...

  4. 自定义CRM系统

    写在前面 之前在windows上写代码逻辑.搞前端等花了很长时间,跑通之后一直没往centos上部署, 昨天尝试部署下,结果发现静态文件找不到 =='' 由于写了2个组件: - arya model的 ...

  5. GeoGlobe Server使用问题收集

    本人在做数字县区过程中,需要吉奥GeoGlobe Server发布数据,期间遇到些平台问题.故立此帖,作为参考. 1 字段限制: GeoGlobe 5.2部署在我的服务器Windows Server ...

  6. SCTP一到多式流分回射服程序

    一.服务器程序 #include <stdlib.h> #include <string.h> #include <strings.h> #include < ...

  7. 调用waitpid的SIGCHLD信号处理函数

    #include <stdio.h> #include <sys/wait.h> void sig_chld(int signo) { pid_t pid; int stat; ...

  8. url 组成部分

    NSURL其实就是我们在浏览器上看到的网站地址,这不就是一个字符串么,为什么还要在写一个NSURL呢,主要是因为网站地址的字符串都比较复杂,包括很多请求参数,这样在请求过程中需要解析出来每个部门,所以 ...

  9. IDAPython教程(三)

    在过去两个部分中,我们已经讨论了使用IDAPython让逆向工程更容易一些.这一部分我们来看一下条件断点. 当在IDA中调试时,分析者经常会遇到希望可以在一个特殊的地址中断下来的情况,但这只有在一些特 ...

  10. WebSocket起航 JavaScript客户端和Server通信

    Message  =>JSON  => Move 客户端发给服务器总是Move  server.send(JSON.stringify({row: row, column: column} ...