★★★简单问题,百分相送★★★ (触发器相关)

puffgeng 2008-06-03 06:55:41
具体内容请参考此贴
http://topic.csdn.net/u/20080509/11/e42f1637-7667-434a-9476-406d8655b115.html

具体问题如下——
我使用的是SQL SERVER 2000服务器,采用了以下触发器
CREATE TRIGGER tblA_Insert ON [tblA] FOR INSERT
as
begin
if exists(select 1 from inserted where len(fld1,4) = 'abcd' )
begin
insert into tblb select * from inserted --如果字段不一样,自己对应改.
delete from tbla where id = (select id from inserted)--假设ID为关键字
end
end

但是这个触发器有一个致命的缺陷,就是每次只能处理一条insert的记录,如果同时插入多条记录,此触发器则无法处理,请问如何优化此触发器,使其可以处理多条记录同时插入这样的事件?
...全文
115 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ws_hgo 2008-06-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 arrow_gx 的回复:]
SQL code
CREATE TRIGGER tblA_Insert ON [tblA] FOR INSERT 
as
begin
if exists(select 1 from inserted where left(fld1,4) = 'abcd' )
begin
insert into tblb select * from inserted --如果字段不一样,自己对应改.
delete from tbla where id in (select id from inserted)--假设ID为关键字
end
end

[/Quote]
-狙击手- 2008-06-04
  • 打赏
  • 举报
回复
CREATE TRIGGER tblA_Insert ON [tblA] FOR INSERT
as
begin
insert into tblb select * from inserted where len(fld1,4)='abcd'
delete taba where id=(select id from inserted where len(fld1,4)='abcd')
end

-晴天 2008-06-04
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 zzyyc 的回复:]
SQL的触发器是表级触发器,DML影响一行或无数行触发动作只触发一次
INSERT INTO Barn_Branch(......)select ....... from tab
语句执行时触发器无效,插入许多行可能导致单个的触发器调用。
[/Quote]

插入多少条,它也都在 inserted 表里.只要运用inserted表,没问题的.
zzyyc 2008-06-04
  • 打赏
  • 举报
回复
SQL的触发器是表级触发器,DML影响一行或无数行触发动作只触发一次
INSERT INTO Barn_Branch(......)select ....... from tab
语句执行时触发器无效,插入许多行可能导致单个的触发器调用。
pt1314917 2008-06-03
  • 打赏
  • 举报
回复

--直接这样?
CREATE TRIGGER tblA_Insert ON [tblA] FOR INSERT
as
begin
insert into tblb select * from inserted where len(fld1,4)='abcd'
delete taba where id=(select id from inserted where len(fld1,4)='abcd')
end
正宗老冉 2008-06-03
  • 打赏
  • 举报
回复
批量用游标循环处理.
中国风 2008-06-03
  • 打赏
  • 举报
回复
CREATE TRIGGER tblA_Insert ON [tblA]
instead of insert
as
begin
insert tblA select * from inserted where fld1 not like 'abcd%'
insert tblB select * from inserted where fld1 like 'abcd%'
end

用这样就可以实现楼主的了
点点星灯 2008-06-03
  • 打赏
  • 举报
回复


但是这个触发器有一个致命的缺陷,就是每次只能处理一条insert的记录,如果同时插入多条记录,此触发器则无法处理,请问如何优化此触发器,使其可以处理多条记录同时插入这样的事件?


不会有这个问题的存在!
arrow_gx 2008-06-03
  • 打赏
  • 举报
回复
CREATE TRIGGER tblA_Insert ON [tblA] FOR INSERT 
as
begin
if exists(select 1 from inserted where left(fld1,4) = 'abcd' )
begin
insert into tblb select * from inserted --如果字段不一样,自己对应改.
delete from tbla where id in (select id from inserted)--假设ID为关键字
end
end
中国风 2008-06-03
  • 打赏
  • 举报
回复
--加条件就可以批量更新了
中国风 2008-06-03
  • 打赏
  • 举报
回复
CREATE TRIGGER tblA_Insert ON [tblA] FOR INSERT
as
begin
if exists(select 1 from inserted where left(fld1,4) = 'abcd' )
begin
insert into tblb select * from inserted where left(fld1,4) = 'abcd' --加上条件
delete from tbla where id in (select id from inserted
end
end
中国风 2008-06-03
  • 打赏
  • 举报
回复


CREATE TRIGGER tblA_Insert ON [tblA] FOR INSERT
as
begin
if exists(select 1 from inserted where len(fld1,4) = 'abcd' )
begin
insert into tblb select * from inserted where len(fld1,4) = 'abcd' --加上条件
delete from tbla where id in (select id from inserted
end
end
jobine 2008-06-03
  • 打赏
  • 举报
回复
len(fld1,4) = 'abcd' ???
是left(fld1,4) = 'abcd' 吧。
另外where id = (select id from inserted)这样可以通过编译?
arrow_gx 2008-06-03
  • 打赏
  • 举报
回复
改一下 delete 的条件 = 改成 in

试试看如何

CREATE TRIGGER tblA_Insert ON [tblA] FOR INSERT 
as
begin
if exists(select 1 from inserted where len(fld1,4) = 'abcd' )
begin
insert into tblb select * from inserted --如果字段不一样,自己对应改.
delete from tbla where id in (select id from inserted)--假设ID为关键字
end
end

22,209

社区成员

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

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