This example creates two tables - tblClients and tblClients_Trigger.
With the COLUMNS_UPDATED() function, we can quickly test for any changes made to columns containing client information.
Using COLUMNS_UPDATED() this way only works when we are trying to detect changes to the first eight columns in the table.
If the column number is greater than 8, then we use
SUBSTRING(COLUMNS_UPDATED(),(((@COL_NO-1/8) + 1),1) & POWER(2,((@COL_NO-1)%8))>0)
Example
DECLARE @COL_NO INT = 15 (More than 8)
IF(SUBSTRING(COLUMNS_UPDATED(),(((@COL_NO-1)/8)+1),1) & POWER(2,((@COL_NO-1)%8))>0))
PRINT 'Column No 15 Updated'
First, create the given two tables in the database.
- CREATE TABLE [dbo].[tblClients](
- [fldGuid] [uniqueidentifier] NULL,
- [fldClientID] [int] NULL,
- [fldClientName] [nvarchar](50) NULL,
- [fldDateCreated] [datetime] NULL,
- [fldCellPhone] [nvarchar](30) NULL,
- [fldEMail] [nvarchar](60) NULL,
- [fldAddress] [nvarchar](60) NULL,
- [fldState] [nvarchar](60) NULL,
- [fldCountry] [nvarchar](60) NULL,
- [fldPK] [int] IDENTITY(1,1) NOT NULL,
- CONSTRAINT [tblClients_pk] PRIMARY KEY NONCLUSTERED
- (
- [fldPK] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- )
- CREATE TABLE [dbo].[tblClients_Trigger](
- [fldGuid] [uniqueidentifier] NULL,
- [fldClientID] [int] NULL,
- [fldClientName] [nvarchar](50) NULL,
- [fldDateCreated] [datetime] NULL,
- [fldCellPhone] [nvarchar](30) NULL,
- [fldEMail] [nvarchar](60) NULL,
- [fldAddress] [nvarchar](60) NULL,
- [fldState] [nvarchar](60) NULL,
- [fldCountry] [nvarchar](60) NULL,
- [fldPK] [int] NOT NULL,
- CONSTRAINT [tblClients_Trigger_pk] PRIMARY KEY NONCLUSTERED
- (
- [fldPK] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- )

Join the conversation! Your thoughts help the community grow.