很多人发短信问这个问题,我不想一一作答。
columns_updated()可能联机帮助上说不大清楚。我写个例子放在这儿。
对大部份人来说很简单,但就是有好多人问相同的问题。
我以前发过,原贴找不着了,好在有保存,现重贴。
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
);
go
insert into t values (
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17
);
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 '第'+rtrim(@i)+'列修改'
set @i=@i-1
end
end
go
update t set col17=1,col15=1,col16=1,col14=1,col09=1,col11=1,col01=1,col02=1,col03=1
go
/
结果:
第17列修改
第16列修改
第15列修改
第14列修改
第11列修改
第9列修改
第3列修改
第2列修改
第1列修改
*/
--修改一下方式:
alter 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
end
go
update t set col17=1,col15=1,col16=1,col14=1,col09=1,col11=1,col01=1,col02=1,col03=1
/*
结果:
列"Col17"修改
列"Col16"修改
列"Col15"修改
列"Col14"修改
列"Col11"修改
列"Col09"修改
列"Col03"修改
列"Col02"修改
列"Col01"修改
*/
---------------------------------------------------------------
support!
---------------------------------------------------------------
learn it
---------------------------------------------------------------
backup....
---------------------------------------------------------------
my english is poor~
ding
---------------------------------------------------------------
up
---------------------------------------------------------------
support!
---------------------------------------------------------------
支持
---------------------------------------------------------------
指出J老师一个小错误!
多了一个END!
---------------------------------------------------------------
哦,没有多,是我自己弄错了!不好意思!
---------------------------------------------------------------
多谢J老师!
---------------------------------------------------------------
学习~!
---------------------------------------------------------------
学习~!
---------------------------------------------------------------
j老师能不能说说原理?谢谢了.
---------------------------------------------------------------
学习
---------------------------------------------------------------
IF ( COLUMNS_UPDATED() & 2 = 2 )
PRINT 'Column b Modified'
在例子有这么一个,是判断第二列有没有更新
但是IF ( COLUMNS_UPDATED() & 3 = 3 )
为什么不能判断第三列有没有更新
这种方法怎么判断其它的列?
---------------------------------------------------------------
good,I like ,I learn
---------------------------------------------------------------
多谢J老师!
---------------------------------------------------------------
先收藏
---------------------------------------------------------------
收藏
---------------------------------------------------------------
根据j老师的例子 返回的COLUMNS_UPDATED() 值为
0x07E501
转换成二进制表示为
00000111 ¦ 11100101 ¦ 00000001
MSDN对COLUMNS_UPDATED()有这样的解释
However, if there are more than eight columns, the COLUMNS_UPDATED() function returns the bytes in order from left to right, with the least significant byte being the leftmost. The leftmost byte will contain information about columns 1 through 8, the second byte will contain information about columns 9 through 16, and so on. If there were nine columns in the table and you want to check if columns 2, 3, or 4 have been updated, the correct bitmask to use is 0x0E00 (decimal 3584).
我们就能依次得到修改的列位置
先取左面的8位00000111 就是修改了列 1 2 3
再取依次8位11100101就是修改了列 9 11 14 15 16
最后的8位00000001得到列 17