这个触发器怎么建?

jamesyue2008 2009-08-15 06:45:50
表1
Classid nameid record
101 111 99
101 111 80
101 111 88
102 113 66
102 113 55

表2
Classid nameid
101 111
102 113

在表1中增加记录时, 想在表1中建立一个触发器, 在表2中收集唯一的 Classid, nameid.
先谢谢了!

...全文
77 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jamesyue2008 2009-08-15
  • 打赏
  • 举报
回复
碰到新问题了,
表1
Classid nameid record
101001 111 99
101002 111 80
101003 111 88
102001 113 66
102002 113 55

表2
Cid Nid
101 111
102 113

在表1中增加记录时, 想在表1中建立一个触发器, 得到表2中的结果"唯一的Cid和Nid". 两表字段名不同。
Cid=Classid/1000。 我这个菜鸟不会举一反三。

再谢谢了!
lihan6415151528 2009-08-15
  • 打赏
  • 举报
回复

create table test1 (classid int,nameid int ,record int)
insert test1
select 101,111,99 union all
select 101,111,80 union all
select 101,111,88 union all
select 102,113,66 union all
select 102,113,55
go

create table test2 (classid int,nameid int)
go



create trigger t_test
on test1
for insert, update
as
begin
insert into test2
select distinct classid,nameid
from inserted t
where not exists(select 1 from test2 where classid=t.classid and nameid=t.nameid)
end

insert into test1
select 103,114,44

select * from test1
select * from test2


classid nameid record
----------- ----------- -----------
101 111 99
101 111 80
101 111 88
102 113 66
102 113 55
103 114 44

(所影响的行数为 6 行)

classid nameid
----------- -----------
103 114

(所影响的行数为 1 行)
Zoezs 2009-08-15
  • 打赏
  • 举报
回复

create trigger t_collect
on tb1
for insert,update
as
begin
insert into tb2
select distinct classid,nameid
from inserteb t
where not exists(select 1 from tb2 where classid=t.classid and nameid=t.nameid)
end

顶老师
  • 打赏
  • 举报
回复
create trigger t_collect
on tb1
for insert,update
as
begin
insert into tb2
select distinct classid,nameid
from inserteb t
where not exists(select 1 from tb2 where classid=t.classid and nameid=t.nameid)
end
jamesyue2008 2009-08-15
  • 打赏
  • 举报
回复
谢谢各位
昵称被占用了 2009-08-15
  • 打赏
  • 举报
回复
帮你写全算了

CREATE TRIGGER FreightIn ON [dbo].[ordship] 
FOR INSERT, UPDATE
AS
insert into Freight (invoice, skuid)
select distinct inv , detilid/10000000 from inserted t
where not exists(select 1 from Freight where invoice=t.inv and skuid=t.detilid/10000000)
昵称被占用了 2009-08-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jamesyue2008 的回复:]
CREATE TRIGGER FreightIn ON [dbo].[ordship]
FOR INSERT, UPDATE
AS
declare @P1 int, @P2 tinyint
select @P1= inv, @P2=detilid/10000000 from inserted
insert into Freight (invoice, skuid)
select distinct @P1 , @P2  from inserted t
where not exists(select 1 from Freight where invoice=@P1 and skuid=@P2)
自己搞定了, 但我没用Begin....end. 不知道有没有后遗症.
[/Quote]
Begin....end.加不加没关系
但是你修改的触发器有严重问题,用了变量的后果是,当你一次插入多条记录的时候,你收集的数据将不全,即会丢失数据,你参考7楼再改吧,(我用了/1000,你的代码用了/1000000)

昵称被占用了 2009-08-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jamesyue2008 的回复:]
碰到新问题了,
表1
Classid    nameid  record
101001        111        99
101002        111        80
101003        111        88
102001        113        66
102002        113        55

表2
Cid    Nid
101    111
102    113

在表1中增加记录时, 想在表1中建立一个触发器, 得到表2中的结果"唯一的Cid和Nid". 两表字段名不同。
Cid=Classid/1000。 我这个菜鸟不会举一反三。

再谢谢了!

[/Quote]

create trigger t_collect
on tb1
for insert,update
as
begin
insert into tb2(cid,nid)
select distinct classid/1000 as cid,nameid as nid
from inserteb t
where not exists(select 1 from tb2 where cid=t.classid/1000 and nid=t.nameid)
end

feixianxxx 2009-08-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jamesyue2008 的回复:]
CREATE TRIGGER FreightIn ON [dbo].[ordship]
FOR INSERT, UPDATE
AS
declare @P1 int, @P2 tinyint
select @P1= inv, @P2=detilid/10000000 from inserted
insert into Freight (invoice, skuid)
select distinct @P1 , @P2  from inserted t
where not exists(select 1 from Freight where invoice=@P1 and skuid=@P2)
自己搞定了, 但我没用Begin....end. 不知道有没有后遗症.
[/Quote]
最好加上
jamesyue2008 2009-08-15
  • 打赏
  • 举报
回复
CREATE TRIGGER FreightIn ON [dbo].[ordship]
FOR INSERT, UPDATE
AS
declare @P1 int, @P2 tinyint
select @P1= inv, @P2=detilid/10000000 from inserted
insert into Freight (invoice, skuid)
select distinct @P1 , @P2 from inserted t
where not exists(select 1 from Freight where invoice=@P1 and skuid=@P2)
自己搞定了, 但我没用Begin....end. 不知道有没有后遗症.

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧