home
-- Columns_updated() more than 32 fields
Author Nigel Rivett
This code creates a table with 50 fields
Adds a single row.
Creates a trigger on the table to disply the fields that have been updated.
Runs some updates to test the result.
Note that the bit test operator works on an int which will only hold the bit map for 32 fields.
The code uses substring to test the correct bits.
drop table trigtest
go
create table trigtest
(
i01 int
)
declare @i int, @s varchar(1000)
select @i = 1
while @i < 50
begin
select @i = @i + 1
select @s = 'alter table trigtest add i' + right('00' + convert(varchar(2),@i),2) + ' int'
exec (@s)
end
go
declare @i int, @s varchar(1000)
select @i = 0
while @i < 50
begin
select @i = @i + 1
select @s = coalesce(@s + ',1', 'insert trigtest select 1')
end
exec (@s)
go
drop trigger tr_trigtest
go
create trigger tr_trigtest on trigtest for update
as
declare @bit int ,
@field int ,
@char int
select @field = 0
while @field < (select max(colid) from syscolumns where id = (select id from sysobjects where name = 'trigtest'))
begin
select @field = @field + 1
select @bit = (@field - 1 )% 8 + 1
select @bit = power(2,@bit - 1)
select @char = ((@field - 1) / 8) + 1
--select @char, @field, @bit -- debug code to check the bits that are tested.
if substring(COLUMNS_UPDATED(),@char, 1) & @bit > 0
select @field, name from syscolumns where colid = @field and id = (select id from sysobjects where name = 'trigtest')
end
go
set nocount on
update trigtest set i08 = 2
update trigtest set i41 = 2
update trigtest set i03 = 2
update trigtest set i40 = 2
update trigtest set i01 = 2
home