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

<<  February 2018  >>
MoTuWeThFrSaSu
2930311234
567891011
12131415161718
19202122232425
2627281234
567891011

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 2018

Sign in