I wish to make my trigger more efficient in that it only processes
columns that have actually changed.
I am currently processing 9 columns unconditionally .
I found UPDATE() function that returns true if a column has changed,
but then I found COLUMNS_UPDATED () which returns a bit mask of the
columns that changed.
So far, I have the trigger only clean the columns only if ANY of the
fields change, but I want to get a bit smarter and only clean a field
if it changes. I cant think of a way to conditionally use the set nor
use combinations of tests.
Any Ideas?
CREATE TRIGGER [dbo].[Clean_Talisma_L ead_Raw]
ON [dbo].[Talisma_Lead_Ra w_tbl]
for insert,update
AS
BEGIN
if update(first) or
update(last) or
update(address2 ) or
update(address2 ) or
update(address3 ) or
update(city) or
update(state) or
update(email) or
update(zip)
BEGIN
update Strayer_Staging .dbo.Talisma_Le ad_Raw_tbl
set
first = dbo.udf_CleanAl phaNum(inserted .first),
last = dbo.udf_CleanAl phaNum(inserted .last),
address1 = dbo.udf_CleanAl phaNum(inserted .Address1),
address2 = dbo.udf_CleanAl phaNum(inserted .Address2),
address3 = dbo.udf_CleanAl phaNum(inserted .Address3),
City = dbo.udf_CleanAl phaNum(inserted .City),
state = dbo.udf_CleanAl phaNum(inserted .state),
email = dbo.udf_CleanAl phaNum(inserted .email),
Zip = dbo.udf_CleanAl phaNum(inserted .Zip),
bad_email = case when rtrim(inserted. email) = '' or
inserted.email is null then null else case when
dbo.ValidateEma ilAddress(lower (replace(insert ed.email,' ',''))) = 0
then 1 else 0 end end
from inserted
where Talisma_Lead_Ra w_tbl.Student_I nsight_ID =
inserted.Studen t_Insight_ID
END
END
columns that have actually changed.
I am currently processing 9 columns unconditionally .
I found UPDATE() function that returns true if a column has changed,
but then I found COLUMNS_UPDATED () which returns a bit mask of the
columns that changed.
So far, I have the trigger only clean the columns only if ANY of the
fields change, but I want to get a bit smarter and only clean a field
if it changes. I cant think of a way to conditionally use the set nor
use combinations of tests.
Any Ideas?
CREATE TRIGGER [dbo].[Clean_Talisma_L ead_Raw]
ON [dbo].[Talisma_Lead_Ra w_tbl]
for insert,update
AS
BEGIN
if update(first) or
update(last) or
update(address2 ) or
update(address2 ) or
update(address3 ) or
update(city) or
update(state) or
update(email) or
update(zip)
BEGIN
update Strayer_Staging .dbo.Talisma_Le ad_Raw_tbl
set
first = dbo.udf_CleanAl phaNum(inserted .first),
last = dbo.udf_CleanAl phaNum(inserted .last),
address1 = dbo.udf_CleanAl phaNum(inserted .Address1),
address2 = dbo.udf_CleanAl phaNum(inserted .Address2),
address3 = dbo.udf_CleanAl phaNum(inserted .Address3),
City = dbo.udf_CleanAl phaNum(inserted .City),
state = dbo.udf_CleanAl phaNum(inserted .state),
email = dbo.udf_CleanAl phaNum(inserted .email),
Zip = dbo.udf_CleanAl phaNum(inserted .Zip),
bad_email = case when rtrim(inserted. email) = '' or
inserted.email is null then null else case when
dbo.ValidateEma ilAddress(lower (replace(insert ed.email,' ',''))) = 0
then 1 else 0 end end
from inserted
where Talisma_Lead_Ra w_tbl.Student_I nsight_ID =
inserted.Studen t_Insight_ID
END
END
Comment