首先真诚感谢大家的帮助!! 问: if @@rowcount!=0 begin insert 另一个表 values(?) 怎么逐个比较呢?inserted、 deleted里有新的和旧的数据,但怎么能取到单个字段?又怎么比较,能不能给出具体的程序?谢谢。 TO 流云:columns_updated()的功能我查看了下帮助功能,但看得不是很懂,像它说的,如果更改了第二、第三、第四列的话,就是:columns_updated()&14,是什么意思,解释是POWER(2,(2-1)+POWER(2,(3-1))+POWER(2,(4-1)=14,能不能解释一下。[email protected]
create table t ( Col01 int , Col02 int , Col03 int , Col04 int , Col05 int , Col06 int , Col07 int , Col08 int , Col09 int , Col10 int , Col11 int , Col12 int , Col13 int , Col14 int , Col15 int , Col16 int , Col17 int ); insert into t values ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 );create trigger tri_test on t for update as begin declare @i int select @i=count(*) from syscolumns where id=object_id('t') while @i>0 begin if substring( columns_updated() , (@i-1)/8+1 , 1 ) & power( 2, (@i-1)%8 ) > 0 print '第'+rtrim(@i)+'列修改' set @i=@i-1 end endupdate t set col17=1,col15=1,col16=1,col14=1,col09=1,col11=1,col01=1,col02=1,col03=1结果: 第17列修改 第16列修改 第15列修改 第14列修改 第11列修改 第9列修改 第3列修改 第2列修改 第1列修改drop trigger tri_test go create trigger tri_test on t for update as begin declare @i int select @i=count(*) from syscolumns where id=object_id('t') while @i>0 begin if substring( columns_updated() , (@i-1)/8+1 , 1 ) & power( 2, (@i-1)%8 ) > 0 print col_name(object_id('t'),@i)+'列修改' set @i=@i-1 end endgoupdate t set col17=1,col15=1,col16=1,col14=1,col09=1,col11=1,col01=1,col02=1,col03=1Col17列修改 Col16列修改 Col15列修改 Col14列修改 Col11列修改 Col09列修改 Col03列修改 Col02列修改 Col01列修改
问:
if @@rowcount!=0
begin
insert 另一个表
values(?)
怎么逐个比较呢?inserted、 deleted里有新的和旧的数据,但怎么能取到单个字段?又怎么比较,能不能给出具体的程序?谢谢。
TO 流云:columns_updated()的功能我查看了下帮助功能,但看得不是很懂,像它说的,如果更改了第二、第三、第四列的话,就是:columns_updated()&14,是什么意思,解释是POWER(2,(2-1)+POWER(2,(3-1))+POWER(2,(4-1)=14,能不能解释一下。[email protected]
Col01 int , Col02 int , Col03 int , Col04 int ,
Col05 int , Col06 int , Col07 int , Col08 int ,
Col09 int , Col10 int , Col11 int , Col12 int ,
Col13 int , Col14 int , Col15 int , Col16 int ,
Col17 int
);
insert into t values (
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17
);create trigger tri_test on t for
update
as
begin
declare @i int
select @i=count(*) from syscolumns where id=object_id('t')
while @i>0
begin
if substring( columns_updated() , (@i-1)/8+1 , 1 ) & power( 2, (@i-1)%8 ) > 0
print '第'+rtrim(@i)+'列修改'
set @i=@i-1
end
endupdate t set col17=1,col15=1,col16=1,col14=1,col09=1,col11=1,col01=1,col02=1,col03=1结果:
第17列修改
第16列修改
第15列修改
第14列修改
第11列修改
第9列修改
第3列修改
第2列修改
第1列修改drop trigger tri_test
go
create trigger tri_test on t for
update
as
begin
declare @i int
select @i=count(*) from syscolumns where id=object_id('t')
while @i>0
begin
if substring( columns_updated() , (@i-1)/8+1 , 1 ) & power( 2, (@i-1)%8 ) > 0
print col_name(object_id('t'),@i)+'列修改'
set @i=@i-1
end
endgoupdate t set col17=1,col15=1,col16=1,col14=1,col09=1,col11=1,col01=1,col02=1,col03=1Col17列修改
Col16列修改
Col15列修改
Col14列修改
Col11列修改
Col09列修改
Col03列修改
Col02列修改
Col01列修改