/*
关于触发器
--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
关于触发器
--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
我都是这样处理的?请教 w_rose(w_rose)
welyngj(平平淡淡) ( )
怎么样修改触发器呢谢谢
向你们学习
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字段的表的时候)或者有时结果并不对(当多用户时)。
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
细心学习中谢谢
怎么办?即
insert test2 values(@@identity,'??')
insert test2 values(@@identity,'??')
insert test2 values(@@identity,'??')??表示插入的值要按某一个条件,不是规则的'11','22','33'请教谢谢
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请问该怎么写?
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。
------------>>
并建立唯一索引
常常被误会,哎,改名去了
当然,也不是和larruping(思无邪) 同个人学习一下思路,谢谢了