1

I am looking at creating a trigger on a live SQL Server spatial table that will update a column when a user either:

  1. Creates a new feature
  2. Updates the geometry of a feature in a GIS application

Unfortunately I have only been able to work out updating a column when a new feature is created, or updating a column when the geometry column is changed, but not both within one trigger.

Ideal world - attribute Updated populates current date for the particular feature that has been created or modified.

This is what I have so far that works only when creating a new feature:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER *[triggername]* ON *[table]* 
AFTER INSERT,UPDATE
AS BEGIN
 DECLARE @ID VARCHAR(8)
 SELECT @ID = [ID] FROM *[table]*
 UPDATE *[table]*
 SET Updated = GETDATE()
 WHERE [ID] = @ID
END
GO

This works when the geometry is changed

CREATE TRIGGER [triggername] ON [table] 
FOR INSERT, UPDATE 
AS BEGIN
 UPDATE [table] 
 SET [table].Updated = Getdate()
 FROM [table] 
 INNER JOIN deleted d ON [table].id = d.id 
END
GO
marc_s
9,0626 gold badges46 silver badges52 bronze badges
asked May 6, 2015 at 23:41

1 Answer 1

5

Isn't this what you're looking for ?

CREATE TRIGGER [triggername] ON [table] 
AFTER INSERT, UPDATE 
AS 
BEGIN
 UPDATE [table] 
 SET [table].Updated = Getdate() 
 WHERE ID IN (SELECT ID FROM INSERTED)
END
GO
marc_s
9,0626 gold badges46 silver badges52 bronze badges
answered May 7, 2015 at 0:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.