简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM