I hope this is a simple enough question for any SQL people out there...
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 colum 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.
Any help you could offer would be appreciated.
OK - as predicted by Cade Roux, this fail spectacularly on multiple updates. I'm going to have to take a new approach to this. Does anyone have any other suggestions, please?
Guys - Please educate me here... 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.
The activation of a trigger results in the running of its associated triggered action.
No there is no provision of having trigger on SELECT operation. As suggested in earlier answer, write a stored procedure which takes parameters that are fetched from SEECT query and call this procedure after desired SELECT query.
SQL Server has three types of triggers: DML (Data Manipulation Language) Triggers. DDL (Data Definition Language) Triggers. Logon Triggers.
The trigger event that initiates the trigger action can be an INSERT, DELETE, UPDATE, or a SELECT statement.
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
How about this?
CREATE TRIGGER [dbo].[SystemParameterInsertUpdate] ON [dbo].[SystemParameter] FOR INSERT, UPDATE AS BEGIN SET NOCOUNT ON IF (LEFT((SELECT Attribute FROM INSERTED), 7) <> 'NoHist_') BEGIN INSERT INTO SystemParameterHistory ( Attribute, ParameterValue, ParameterDescription, ChangeDate ) SELECT Attribute, ParameterValue, ParameterDescription, ChangeDate FROM Inserted AS I END END
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With