patternMinor
Insert if something changed other than specific column
Viewed 0 times
insertcolumnthanspecificotherchangedsomething
Problem
I have a table with many columns but my snippet is simplified. I have the trigger shown below but how can I have it so that nothing is inserted if only the
I could do a
EDIT
I've tried adding
but that doesn't work either. I thought that would be the naive method but that doesn't work.
created_at column has changed?ALTER TRIGGER [dbo].[t_upd_insert]
ON [dbo].[Player]
FOR INSERT, UPDATE
AS
SET XACT_ABORT, NOCOUNT ON;
BEGIN
INSERT INTO dbo.Changes (
[name]
[created_at]
)
SELECT
ins.[name],
ins.[created_at]
FROM INSERTED insI could do a
WHERE name != ins.[name] for each column but there are a lot of columns and tables that need this; is there a shorter syntax?EDIT
I've tried adding
WHERE
[name] != ins.[name] OR
[age] != ins.[age]
....but that doesn't work either. I thought that would be the naive method but that doesn't work.
Solution
ALTER TRIGGER [dbo].[t_upd_insert]
ON [dbo].[Player]
FOR INSERT, UPDATE
AS
SET XACT_ABORT, NOCOUNT ON;
BEGIN
IF (update(name))
BEGIN
INSERT INTO dbo.Changes (
[name]
[created_at]
)
SELECT
ins.[name],
ins.[created_at]
FROM INSERTED ins
ENDCode Snippets
ALTER TRIGGER [dbo].[t_upd_insert]
ON [dbo].[Player]
FOR INSERT, UPDATE
AS
SET XACT_ABORT, NOCOUNT ON;
BEGIN
IF (update(name))
BEGIN
INSERT INTO dbo.Changes (
[name]
[created_at]
)
SELECT
ins.[name],
ins.[created_at]
FROM INSERTED ins
ENDContext
StackExchange Database Administrators Q#142542, answer score: 2
Revisions (0)
No revisions yet.