|
-
Jan 31st, 2007, 04:08 AM
#1
Thread Starter
Hyperactive Member
Firing trigger when only one column is changed
Hi All,
A nice person wrote this for me yesterday
Code:
CREATE TRIGGER dbo.gmupstaus
ON contact1
for update
as
Update Contact2 Set
uprospect = Case When Inserted.Key1 = 'Prospect' and uprospect is null Then GetDate() Else uprospect End,
UFCSDATE = Case When Inserted.Key1 = 'Trial' and UFCSDATE is null Then GetDate() Else UFCSDATE End,
USTARTDATE = Case When Inserted.Key1 = 'Client' and USTARTDATE is null Then GetDate() Else USTARTDATE End
From Contact2 Inner Join
Inserted On Contact2.AccountNo = Inserted.AccountNo And Inserted.Key1 Is Not Null
I would just like to make it a bit more effiicent as the update stament only needs to fire if key1 changes.
Have looked on the help files and this seems appropriate. I just don't understand the whole substring waffle. The column I'm interested in is column 25.
F. Use COLUMNS_UPDATED to test more than 8 columns
If you must test for updates that affect columns other than the first 8 columns in a table, you must use the SUBSTRING function to test the proper bit returned by COLUMNS_UPDATED. This example tests for updates that affect columns 3, 5, or 9 in the Northwind.dbo.Customers table.
USE Northwind
DROP TRIGGER tr1
GO
CREATE TRIGGER tr1 ON Customers
FOR UPDATE AS
IF ( (SUBSTRING(COLUMNS_UPDATED(),1,1)=power(2,(3-1))
+ power(2,(5-1)))
AND (SUBSTRING(COLUMNS_UPDATED(),2,1)=power(2,(1-1)))
)
PRINT 'Columns 3, 5 and 9 updated'
GO
UPDATE Customers
SET ContactName=ContactName,
Address=Address,
Country=Country
GO
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|