Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Refer to Column by position in a trigger
Message
From
10/07/2001 17:00:02
 
 
To
10/07/2001 10:22:56
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Miscellaneous
Thread ID:
00528593
Message ID:
00528915
Views:
16
IF (COLUMNS_UPDATED())
Tests, in an INSERT or UPDATE trigger only, whether the mentioned column or columns were inserted or updated. COLUMNS_UPDATED returns a varbinary bit pattern that indicates which columns in the table were inserted or updated.
COLUMNS_UPDATED can be used anywhere inside the body of the trigger.

bitwise_operator
Is the bitwise operator to use in the comparison.
updated_bitmask
Is the integer bitmask of those columns actually updated or inserted. For example, table t1 contains columns C1, C2, C3, C4, and C5. To check whether columns C2, C3, and C4 are all updated (with table t1 having an UPDATE trigger), specify a value of 14. To check whether only column C2 is updated, specify a value of 2.
comparison_operator
Is the comparison operator. Use the equals sign (=) to check whether all columns specified in updated_bitmask are actually updated. Use the greater than symbol (>) to check whether any or some of the columns specified in updated_bitmask are updated.
column_bitmask
Is the integer bitmask of those columns to check whether they are updated or inserted.


>I have this portion of a trigger:
>
>
>IF UPDATE("KEY1")
>BEGIN
>	INSERT INTO PDMLOG (USERNAME,TABLENAME,FIELDNAME,NEWVALUE,
>		OLDVALUE,UPDATETYPE,UPDATEDATE,ID)
>	SELECT
>		USER_NAME(),'STYLES','KEY1',i.key1,d.key1,@op
>		,GETDATE(),i.pk_recno
>		FROM inserted i LEFT OUTER JOIN deleted d
>		ON i.pk_recno=d.pk_recno
>END
>
>IF UPDATE("KEY2")
>BEGIN
>	INSERT INTO PDMLOG (USERNAME,TABLENAME,FIELDNAME,NEWVALUE,
>		OLDVALUE,UPDATETYPE,UPDATEDATE,ID)
>	SELECT
>		USER_NAME(),'STYLES','KEY2',i.key2,d.key2,@op
>		,GETDATE(),i.pk_recno
>		FROM inserted i LEFT OUTER JOIN deleted d
>		ON i.pk_recno=d.pk_recno
>END
>
>
>...
>
>It continues on with a list of a bunch of columns..
>
>What I would like to do is be able to loop through the columns in this
>table so that it doesn't matter of column names change and also so the trigger
>can be used on other tables is there a way to do something like:
>
>FOR i=1 to ColumnCount
>IF UPDATE(column(i))
>BEGIN
>
>	INSERT INTO PDMLOG (USERNAME,TABLENAME,FIELDNAME,NEWVALUE,
>		OLDVALUE,UPDATETYPE,UPDATEDATE,ID)
>	SELECT
>		USER_NAME(),'STYLES',column(i).value,i.column(i),d.column(i),@op
>		,GETDATE(),i.pk_recno
>		FROM inserted i LEFT OUTER JOIN deleted d
>		ON i.pk_recno=d.pk_recno
>END
>ENDFOR
>
>
>Sooooo. Is there some way that you can refer to the column by it's ordinal.
>If you get the idea...
>
>Thanks
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform