/*
关于触发器
--test1主表
--test2从表
*/use pubs
gocreate table test1(
id int identity(1,1),
name varchar(100)
)create table test2(
id int,
idname varchar(100)
)create trigger testinsert on test1
for insert
as
begin tran
insert  test2 values(@@identity,'11')
insert  test2 values(@@identity,'22')
insert  test2 values(@@identity,'33')
commit tran/*以下是测试
*/--1.测试1
insert test1 values('aa')
insert test1 values('bb')
insert test1 values('cc')
--以上test2中插入9条记录--2.测试2
insert into test1 select title from titles
--test2中只插入了3条记录/*
修改触发器
*/
alter trigger testinsert on test1
for insert
as
if @@Rowcount=1 --如果是单句insert语句
begin
begin tran
insert  test2 values(@@identity,'11')
insert  test2 values(@@identity,'22')
insert  test2 values(@@identity,'33')
commit tran
end
else --如果是insert into select 语句这样形式的
begin
declare @ii int --记录当前游标所指向的记录的ID号
Declare @dav as varchar(50)   ---定义变量,用来保存从游标中取得的值
Declare  cursor5 cursor for select id from inserted   ---定义游标 
open  cursor5   ---打开游标
fetch  cursor5 into @dav   
while @@fetch_status=0  ---循环读取值
begin
begin tran
set @ii=(select id from inserted where id=@dav)--取得Identity的值
insert test2 values(@ii,'11') 
insert test2 values(@ii,'22') 
insert test2 values(@ii,'33') 
fetch cursor5 into @dav
commit tran
end
close  cursor5  ---关闭游标
Deallocate Cursor5    ---删掉游标
end

解决方案 »

  1.   

    以前对数据表批量插入时在触发器里处理数据
    我都是这样处理的?请教 w_rose(w_rose)
    welyngj(平平淡淡) ( )
    怎么样修改触发器呢谢谢
    向你们学习
      

  2.   

    sorry!俺也疑惑,帮助上是怎么说的。
      

  3.   

    触发器中不需要使用 begin tran和commit tran语句,多此一举。触发器本来就是工作事务中。create trigger testinsert on test1 for insert as
      insert  test2 
        select i.id,'11' from inserted as i union all
        select i.id,'22' from inserted as i union all
        select i.id,'33' from inserted as i
    go
    即使你想得到identity的值,最好也不要使用@@ideintity函数。它有时得到不是你当前插入记录的那个表的值(当他触发器他含有identity字段的表的时候)或者有时结果并不对(当多用户时)。
      

  4.   

    当然也可以写:create trigger testinsert on test1 for insert as
      insert  test2 
        select i.id,x.ch from inserted as i ,(select '11' as ch union all
          select '22' union all select '33') as x
    go
      

  5.   

    另外,insert后边应该写上字段列表,这是好的编程习惯,将来表结构改变了,也不至于出错。呵呵!一看到有人用游标,我的心脏就乱跳得难受。
      

  6.   

    谢谢w_rose(w_rose) 的指点
    细心学习中谢谢
      

  7.   

    想再问一下,如果不是插入规则的'11','22','33'
    怎么办?即
    insert  test2 values(@@identity,'??')
    insert  test2 values(@@identity,'??')
    insert  test2 values(@@identity,'??')??表示插入的值要按某一个条件,不是规则的'11','22','33'请教谢谢
      

  8.   

    例如table1 有字段f1,f2,f3, f3由触发器更加插入的f1,f2相加而成,触发器如下:CREATE TRIGGER [trg_test] ON [dbo].[TABLE1] 
    FOR INSERT
    AS
        update table1 set f3 = inserted.f1 + inserted.f2 FROM inserted如果插入一条记录的话能算出正确的值,但如果用批量插入的话就得不出正确结果了:
    insert table1(f1,f2) select 1,2 
    union all select 2,3
    union all select 2,4
    union all select 2,5请问该怎么写?
      

  9.   

    思无邪和晓露是同一个人么?怎么总是各说半句话?呵呵!玩笑!CREATE TRIGGER [trg_test] ON [dbo].[TABLE1] 
    FOR INSERT
    AS
        update t set f3 = f1 + f2 FROM inserted as i
          inner join table1 as t on i.key=t.key
    go我不知道怎么定位table1中的记录,所以假设为key。在我的设计中,没有不具有主键的表。如果你们的表没有主键,至少可以增加(alter table ... add ...)一个identity型的字段(并建立唯一键)作为这里的key。
      

  10.   

    并建立唯一键
    ------------>>
    并建立唯一索引
      

  11.   

    sorry ,不是晓露MM
    常常被误会,哎,改名去了
    当然,也不是和larruping(思无邪) 同个人学习一下思路,谢谢了