日期:2014-05-18 浏览次数:20736 次
--> 测试数据:[表A]
if object_id('[表A]') is not null drop table [表A]
create table [表A](
[Id] int,
[CREATORID] int,
[SENDTIME] datetime,
[SENDLEVEL] int,
[RF_ID] int
)
insert [表A]
select 131679,3,'2012-03-27 12:46:30.157',2,4928001 union all
select 131680,3,'2012-03-27 12:46:30.157',2,4928002 union all
select 131681,3,'2012-03-27 12:46:30.157',2,4928003 union all
select 131682,3,'2012-03-27 12:46:30.157',2,4928004 union all
select 131683,3,'2012-03-27 12:46:30.157',2,4928005 union all
select 131684,3,'2012-03-27 12:46:30.157',2,4928006
--> 测试数据:[表B]
if object_id('[表B]') is not null drop table [表B]
create table [表B](
[Id] int,
[CREATORID] int,
[SENDTIME] datetime,
[SENDLEVEL] int,
[SENDSTATE] int,
[Template_Id] int
)
insert [表B]
select 4928001,3,'2012-03-26 20:01:30.000',2,0,8 union all
select 4928002,3,'2012-03-26 20:01:30.000',2,0,10 union all
select 4928003,3,'2012-03-26 20:01:30.000',2,0,9 union all
select 4928004,3,'2012-03-26 20:01:30.000',2,0,9 union all
select 4928005,3,'2012-03-26 20:01:30.000',2,0,8 union all
select 4928006,3,'2012-03-26 20:01:30.000',2,0,8
go
CREATE TRIGGER [dbo].[Tri_Insert_Update_Net]
ON [表A]
AFTER INSERT
AS
update [表B]
set [表B].CREATORID=I.CREATORID,
[表B].[SENDTIME]=I.SENDTIME,
[表B].[SENDLEVEL]=I.SENDLEVEL
FROM INSERTED I WHERE I.RF_ID=[表B].ID
insert [表A]
select 131679,3,'2012-03-27 12:46:30.157',2,4928001 union all
select 131680,3,'2012-03-27 12:46:30.157',2,4928002 union all
select 131681,3,'2012-03-27 12:46:30.157',2,4928003 union all
select 131682,3,'2012-03-27 12:46:30.157',2,4928004 union all
select 131683,3,'2012-03-27 12:46:30.157',2,4928005 union all
select 131684,3,'2012-03-27 12:46:30.157',2,4928006
select * from [表B]
/*
Id CREATORID SENDTIME SENDLEVEL SENDSTATE Template_Id
4928001 3 2012-03-27 12:46:30.157 2 0 8
4928002 3 2012-03-27 12:46:30.157 2 0 10
4928003 3 2012-03-27 12:46:30.157 2 0 9
4928004 3 2012-03-27 12:46:30.157 2 0 9
4928005 3 2012-03-27 12:46:30.157 2 0 8
4928006 3 2012-03-27 12:46:30.157 2 0 8
*/
这个结果不对吗?
------解决方案--------------------
if object_id('[表A]') is not null drop table [表A]
create table [表A](
[Id] int,
[CREATORID] int,
[SENDTIME] datetime,
[SENDLEVEL] int,
[RF_ID] int
)
--> 测试数据:[表B]
if object_id('[表B]') is not null drop table [表B]
create table [表B](
[Id] int,
[CREATORID] int,
[SENDTIME] datetime,
[SENDLEVEL] int,
[SENDSTATE] int,
[Template_Id] int
)
insert [表B]
select 4928001,3,'2012-03-26 20:01:30.000',2,0,8 union all
select 4928002,3,'2012-03-26 20:01:30.000',2,0,10 union all
select 4928003,3,'2012-03-26 20:01:30.000',2,0,9 union all
select 4928004,3,'2012-03-26 20:01:30.000',2,0,9 union all
select 4928005,3,'2012-03-26 20:01:30.000',2,0,8 union all
select 4928006,3,'2012-03-26 20:01:30.000',2,0,8
go
CREATE TRIGGER [dbo].[Tri_Insert_Update_Net]
ON [表A]
AFTER INSERT
AS
if exists(select 1 from inserted) and not exists (select 1 from deleted)
begin
update