I wrote several triggers to keep modification times of several tables updated in this fashion:
CREATE TABLE foo (
foo_id INT IDENTITY(1, 1) NOT NULL,
-- [...]
created_on DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_on DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
CONSTRAINT foo_pk PRIMARY KEY (foo_id)
);
CREATE TRIGGER foo_trg1 ON foo
FOR UPDATE
AS BEGIN
UPDATE foo
SET updated_on = CURRENT_TIMESTAMP
FROM INSERTED ins
WHERE foo.foo_id = ins.foo_id;
END;
For whatever reason I don't have time to debug right now, my PHP client code was triggering a SQL error when issuing a series of queries, although it only happened in one specific table:
A trigger returned a resultset and/or was running with SET NOCOUNT OFF while another outstanding result set was active.
I looked up what SET NOCOUNT means and guessed the error would go enabling it in my trigger, as it actually happened:
CREATE TRIGGER foo_trg1 ON foo
FOR UPDATE
AS BEGIN
SET NOCOUNT ON; -- Problem solved!
UPDATE foo
SET updated_on = CURRENT_TIMESTAMP
FROM INSERTED ins
WHERE foo.foo_id = ins.foo_id;
END;
My questions are:
- Is it safe to just add it to all my updated_on triggers?
- Am I right to assume the performance gain will be negligible in this case?