SQL Server Trigger Columns Updated

by ebarcza 11/10/2011 7:00:00 AM
This handy dandy little function will return a table with all the columns that were updated in a trigger.
CREATE FUNCTION [dbo].[udf_GetUpdatedColumns]( 
@Tablename VARCHAR(100), @ColumnsUpdated VARBINARY(255) ) 
RETURNS TABLE 
AS 
RETURN 
    SELECT 
        COLUMN_NAME 
    FROM INFORMATION_SCHEMA.COLUMNS Field 
    WHERE 
        TABLE_NAME = @Tablename 
        AND sys.fn_IsBitSetInBitmask( @ColumnsUpdated,
            COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME), 
            COLUMN_NAME, 'ColumnID')) <> 0

Usage is as follows:

ALTER trigger [dbo].[trg_InfoUpdate] on [dbo].[Info] for  update
as
print 'TRIGGAH'
DECLARE @ColumnsUpdated VARBINARY(1000) = COLUMNS_UPDATED()
SELECT * FROM dbo.udfDecodeBitmask('BankingInfo', @ColumnsUpdated)

Tags: , ,

SQL SERVER

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

Name of author Author name
Something about me and what I do.

E-mail me Send mail

Calendar

<<  September 2017  >>
MoTuWeThFrSaSu
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

View posts in large calendar

Recent comments

Tags

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2017

Sign in