I have created below query to create a trigger to update the lastUpdatedTime
. I need to update the bottom two rows using the trigger below.
But when I use
ORDER BY p.id DESC;
I get an error:
Incorrect syntax near the keyword 'order'
CREATE TRIGGER trgAfterUpdate12
ON [spider3].[ProductClassModulePeriod]
FOR INSERT, UPDATE
AS
UPDATE TOP(2) p
SET [lastUpdatedTime] = GETDATE()
FROM [spider3].[ProductClassModulePeriod] AS p
INNER JOIN inserted AS i ON p.id = i.id;
Tried using WITH but gets below error
CREATE TRIGGER trgAfterUpdate122
ON [spider3].[ProductClassModulePeriod]
FOR INSERT, UPDATE
AS
WITH q AS
(
SELECT *
FROM [spider3].[ProductClassModulePeriod] AS p
INNER JOIN inserted AS i ON p.id = i.id
ORDER BY p.primaryKey DESC
)
UPDATE q
SET [lastUpdatedTime] = GETDATE();
Error:
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
How can I update the bottom 2 rows?