创建表:

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. Spring boot中使用Mongodb

    安装 使用Idea新建Spring boot工程时需要选择Mongodb 或者在工程中加入依赖 Maven: <dependency> <groupId>org.springf ...

  2. Vue 限制input输入 限数字 或 小数点后两位number

    Vue 限制input输入 小数点后两位number <input type="number" @keydown="handleInput2" place ...

  3. C++ 类2

    继承: 联系一起的类有层次关系. 基类: 层次关系的根部 派生类: 基类负责定义在层次关系中所以类共同拥有的成员,派生类定义其各种特有的成员. 基类类型相关的函数, 派生类不做改变直接继承的函数 某些 ...

  4. Coursera, Machine Learning, notes

      Basic theory (i) Supervised learning (parametric/non-parametric algorithms, support vector machine ...

  5. python 装饰器前之闭包和装饰器

      装饰器: 一, 例如: # vim yue7.py def foo(): print ("fool-------------------") foo() 运行: [root@l ...

  6. python栈

    class StackEmptyError(Exception): pass class StackFullError(Exception): pass class Stack: def __init ...

  7. python 的基础 学习第十天函数的初始

    1,什么是函数,函数就是封装一个功能. 怎么定义函数. # def my_len():#def 是关键字,定义一个一个函数.#my_len():就是函数名,必须和关键字加一个空格,后面加括号和冒号.d ...

  8. 织梦自定义表单ajax提交范例

    function add_ajaxmessage(){ var dh = document.getElementById("tel"); //表单验证 if($("#te ...

  9. pycharm 使用教程

    本文为大家讲解的是pycharm 的安装和首次使用,PyCharm 是我用过的python编辑器中,比较顺手的一个.而且可以跨平台,在macos和windows下面都可以用,这点比较好.感兴趣的同学参 ...

  10. Flask与mysql数据库字段类型的区别以及基本用法

    Mysql里面的int在Flask中里面使用Integer Mysql里面的varcahar在Flask中里面使用String 与Mysql数据库需要导入模块 #导入第三方链接库sql点金术 from ...