if object_id('TABLE_1') is not null drop table TABLE_1
CREATE TABLE TABLE_1
(
ID INT primary key,
Name1 nchar(10),
Name2 nchar(10)
)
if object_id('TABLE_2') is not null drop table TABLE_2
CREATE TABLE TABLE_2
(
ID INT primary key,
Name1 nchar(10),
Name2 nchar(10)
)
INSERT INTO Table_1 VALUES(1,'adsd','Chi')
INSERT INTO Table_2 VALUES(1,'Lei','Chi')
INSERT INTO Table_2 VALUES(2,'Alex','Chi')
create trigger tr_info on TABLE_2
for insert,update,delete
as
begin
if exists(select 1 from inserted) and not exists(select 1 from deleted)--insert触发器
begin
insert into Table_1 select * from inserted
end
else if exists(select 1 from inserted) and exists(select 1 from deleted)--update触发器
begin
update b1
set b1.Name1=U.Name1,b1.Name2=U.Name2
from Table_1 b1,deleted U
where b1.ID=U.ID
end
else
begin
delete Table_1 where ID=(select ID from deleted)
end
end
--插入测试
INSERT INTO Table_2 VALUES(3,'huguo','Chi')
select * from TABLE_1
ID Name1 Name2
----------- ---------- ----------
1 adsd Chi
2 Alex Chi
3 huguo Chi
(3 行受影响)
--删除测试
delete from Table_2 where ID=3
ID Name1 Name2
----------- ---------- ----------
1 adsd Chi
2 Alex Chi
(2 行受影响)
--更新测试
select * from Table_2
update Table_2 set Name1='ALex2' where ID=2
ID Name1 Name2
----------- ---------- ----------
1 adsd Chi
2 ALex2 Chi
(2 行受影响)
分享到:
相关推荐
要使触发器触发,WHEN 子句中布尔型表达式的值必须判定为 TRUE 可以将 REPLACE 添加到 CREATE 语句以自动删除和重建触发器 2. 行级触发器中引用表数据 在行级触发器中,使用伪记录来表示旧数据:old和新数据:new 引用...
实例137 使用线程局部变量实现线程同步 177 实例138 简单的线程通信 179 实例139 简单的线程死锁 180 实例140 解决线程的死锁问题 182 6.3 线程的进阶 183 实例141 使用阻塞队列实现线程同步 183 实例142 新建有...
6.6 用ADO操作表 131 6.6.1 查看Tables集合 131 6.6.2 使用字段和索引创建新表 133 6.6.3 通过加入索引来修改现有表 136 6.7 小 结 136 第7章 使用VBA处理Access中的错误 138 7.1 Access中的错误消息 138 7.2...
第1章 开发环境 2 1.1 AppServ——PHP集成化安装包 3 实例001 通过AppServ配置PHP开发环境 3 实例002 AppServ应用技巧 6 实例003 第1个PHP程序 7 1.2 XAMPP——PHP集成化安装包 8 实例004 通过XAMPP配置PHP开发环境 ...
6.6 用ADO操作表 131 6.6.1 查看Tables集合 131 6.6.2 使用字段和索引创建新表 133 6.6.3 通过加入索引来修改现有表 136 6.7 小 结 136 第7章 使用VBA处理Access中的错误 138 7.1 Access中的错误消息 ...