把下面的条件同时换为你的双条件.编号 = t.编号 and 月份 = t.月份 --触发器的操作1create table 化验室纱组(本厂编号 int,客户 int,色号 int,纱支 int) create table 化验室布组(本厂编号 int,客户 int,色号 int,布类 int) go create trigger my_trig on 化验室纱组 for insert ,update ,delete as if not exists(select 1 from inserted) delete 化验室布组 from deleted t where 化验室布组.本厂编号 = t.本厂编号 else if not exists(select 1 from deleted) insert into 化验室布组(本厂编号 ,客户 ,色号) select 本厂编号 ,客户 ,色号 from inserted else update 化验室布组 set 客户 = t.客户 , 色号 = t.色号 from inserted t where 化验室布组.本厂编号 = t.本厂编号 go--1、insert 对化验室纱组插入数据,然后查看化验室布组表的数据 insert into 化验室纱组 values(1 , 2 , 3 , 4) insert into 化验室纱组 values(5 , 6 , 7 , 8) go select * from 化验室布组 /* 本厂编号 客户 色号 布类 ----------- ----------- ----------- ----------- 1 2 3 NULL 5 6 7 NULL(所影响的行数为 2 行) */--2、update , 更改化验室纱组表中本厂编号=1的色号=6 update 化验室纱组 set 色号 = 6 where 本厂编号 = 1 go select * from 化验室布组 /* 本厂编号 客户 色号 布类 ----------- ----------- ----------- ----------- 1 2 6 NULL 5 6 7 NULL(所影响的行数为 2 行) */--3、delete 化验室纱组表中本厂编号=1的那条数据 delete from 化验室纱组 where 本厂编号 = 1 go select * from 化验室布组 /* 本厂编号 客户 色号 布类 ----------- ----------- ----------- ----------- 5 6 7 NULL(所影响的行数为 1 行) */drop table 化验室纱组 , 化验室布组
create trigger trigger_name on a for insert as insert b(有编号,月份) select 有编号,月份 from inserted
create trigger trigger_name on a for insert as insert b(编号,月份) select 编号,月份 from inserted
其实用到的是这个: 说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用) insert into b(a, b, c) select d,e,f from a;
--===================================================== --TB1插入数据同时查数据到TB2 --===================================================== create table TB1(name nvarchar(20),sex bit)create table TB2(name nvarchar(20),sex bit)insert TB1 select 'abc',1 union all select '123',1 union all select 'ABC',1Select * from tb1 /* name sex -------------------- ----- abc 1 123 1 ABC 1 */ create trigger trg_test on TB1 for insert as begin insert into TB2(name,sex) select name,sex from inserted end goinsert TB1 select 'WWW',1Select * from tb1 /* name sex -------------------- ----- abc 1 123 1 ABC 1 WWW 1 */ select * from tb2 /* name sex -------------------- ----- WWW 1 */
--触发器的操作1create table 化验室纱组(本厂编号 int,客户 int,色号 int,纱支 int)
create table 化验室布组(本厂编号 int,客户 int,色号 int,布类 int)
go
create trigger my_trig on 化验室纱组 for insert ,update ,delete
as
if not exists(select 1 from inserted)
delete 化验室布组 from deleted t where 化验室布组.本厂编号 = t.本厂编号
else if not exists(select 1 from deleted)
insert into 化验室布组(本厂编号 ,客户 ,色号) select 本厂编号 ,客户 ,色号 from inserted
else
update 化验室布组 set 客户 = t.客户 , 色号 = t.色号 from inserted t where 化验室布组.本厂编号 = t.本厂编号
go--1、insert 对化验室纱组插入数据,然后查看化验室布组表的数据
insert into 化验室纱组 values(1 , 2 , 3 , 4)
insert into 化验室纱组 values(5 , 6 , 7 , 8)
go
select * from 化验室布组
/*
本厂编号 客户 色号 布类
----------- ----------- ----------- -----------
1 2 3 NULL
5 6 7 NULL(所影响的行数为 2 行)
*/--2、update , 更改化验室纱组表中本厂编号=1的色号=6
update 化验室纱组 set 色号 = 6 where 本厂编号 = 1
go
select * from 化验室布组
/*
本厂编号 客户 色号 布类
----------- ----------- ----------- -----------
1 2 6 NULL
5 6 7 NULL(所影响的行数为 2 行)
*/--3、delete 化验室纱组表中本厂编号=1的那条数据
delete from 化验室纱组 where 本厂编号 = 1
go
select * from 化验室布组
/*
本厂编号 客户 色号 布类
----------- ----------- ----------- -----------
5 6 7 NULL(所影响的行数为 1 行)
*/drop table 化验室纱组 , 化验室布组
for insert
as
insert b(有编号,月份)
select 有编号,月份 from inserted
for insert
as
insert b(编号,月份)
select 编号,月份 from inserted
说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)
insert into b(a, b, c) select d,e,f from a;
--TB1插入数据同时查数据到TB2
--=====================================================
create table TB1(name nvarchar(20),sex bit)create table TB2(name nvarchar(20),sex bit)insert TB1
select 'abc',1 union all
select '123',1 union all
select 'ABC',1Select * from tb1
/*
name sex
-------------------- -----
abc 1
123 1
ABC 1
*/
create trigger trg_test on TB1 for insert
as
begin
insert into TB2(name,sex)
select name,sex from inserted
end
goinsert TB1
select 'WWW',1Select * from tb1
/*
name sex
-------------------- -----
abc 1
123 1
ABC 1
WWW 1
*/
select * from tb2
/*
name sex
-------------------- -----
WWW 1
*/