Hamid Reza
Hamid Reza

Reputation: 2973

How to make a trigger for sql server 2008 after insert?

I have a table named Page and the table has a column named Priority. I want that when I insert a row in the page table the priority column take the value of the inserted row PageId. PageId is my table primary key.

I wrote this trigger for it:

CREATE TRIGGER PagePriority
ON [Page]
AFTER INSERT
AS
Begin
update inserted
set [Priority]=(select PageId from [Page] where Page.PageId=inserted.PageId)
End

But I have some errors on the set line.

How to do this?

Upvotes: 1

Views: 768

Answers (1)

robnick
robnick

Reputation: 1768

Try the following (assumes that PageId is an int)

CREATE TRIGGER PagePriority
ON [Page]
AFTER INSERT
AS
Begin

DECLARE @thePageId integer = 0
SET @thePageId = (SELECT PageId from inserted)

update [Page]
set [Page].Priority = @thePageId where [Page].PageId = @thePageId

End

Upvotes: 1

Related Questions