We have a table which hold system configuration data, and this is tied to a history table via triggers so we can track who changed what, and when.
I have a requirement to add another value in to this table, but it is one that will change frequently from code, and has a requirement that we don't track it's history (we don't want to clog the table with many thousands of updates per day.
At present, our trigger is a little like this...
CREATE TRIGGER
[dbo].[SystemParameterInsertUpdate]
ON
[dbo].[SystemParameter]
FOR INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON
INSERT INTO SystemParameterHistory
(
Attribute,
ParameterValue,
ParameterDescription,
ChangeDate
)
SELECT
Attribute,
ParameterValue,
ParameterDescription,
ChangeDate
FROM Inserted AS I
END
I'd like to be able to add some logic to stop it creating the record if an Attribute column value is prefixed with a specific string (e.g. "NoHist_")
Given that I have almost no experience working with triggers, I was wondering how it would be best to implement this... I have tried a where clause like the following
where I.Attribute NOT LIKE 'NoHist_%'
but it doesn't seem to work. The value is still copied over into the history table.
as predicted by Cade Roux, this fail spectacularly on multiple updates. I'm going to have to take a new approach to this. What new things can I try?
Why would LEFT() be preferable to LIKE in this scenario? I know I've accepted the answer, but I'd like to know for my own education.
Given that a WHERE clause did not work, maybe this will:
CREATE TRIGGER
[dbo].[SystemParameterInsertUpdate]
ON
[dbo].[SystemParameter]
FOR INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON
If (SELECT Attribute FROM INSERTED) LIKE 'NoHist_%'
Begin
Return
End
INSERT INTO SystemParameterHistory
(
Attribute,
ParameterValue,
ParameterDescription,
ChangeDate
)
SELECT
Attribute,
ParameterValue,
ParameterDescription,
ChangeDate
FROM Inserted AS I
END