求一触发器写法,触发多条插入

chengfeng 2006-09-26 11:04:51
当a(字段:ID,...)表插入一条记录时
触发b表插入多条记录(字段:ID,xh)
b.ID=a.ID, xh从1-20
...全文
215 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
rea1gz 2006-09-27
  • 打赏
  • 举报
回复
呵呵,看英文比较累
YaoMing_csdn 2006-09-27
  • 打赏
  • 举报
回复
To rea1gz: I have prompted it on 2nd floor.
Well 2006-09-27
  • 打赏
  • 举报
回复
create trigger InsertMoreData
on a
for insert
as
insert into b
select * from a where id<=20
go
rea1gz 2006-09-27
  • 打赏
  • 举报
回复
1楼的代码有个大问题,如果一次插入多条记录,这个触发器不正常,只插入一个id的20条记录到b表
rea1gz 2006-09-27
  • 打赏
  • 举报
回复
create trigger tr_a_insert
on a
for insert
as
insert b (id,xh)
select i.id,t.xh
from insert i,(
select 1 as xh
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9
union all select 10
union all select 11
union all select 12
union all select 13
union all select 14
union all select 15
union all select 16
union all select 17
union all select 18
union all select 19
union all select 20
) as t

go

YaoMing_csdn 2006-09-26
  • 打赏
  • 举报
回复
I am not sure this script will work. But this is a way you should to think.

This script will not work correct if you insert multi rows to table a.
YaoMing_csdn 2006-09-26
  • 打赏
  • 举报
回复
create trigger t_MyTrigger
on a
after insert
as
begin
declare @xh int
declare @sql varchar(4000)
declare @id int

select @id = [ID] from inserted

set @xh=1
while (@xh<=20)
begin
set @sql='insert into b values('+@id+','+@xh+')'
exec @sql
set @xh=@xh+1
end
end

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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