I need to update the first N rows in a table meeting a condition.
I know I can do an Update Top N... but the problem is that N is in a @variable.
UPDATE TOP @N SET ... doesn't work.
Is there a way to do this that I am just missing?
No specific table definitions here because it doesn't matter what the columns are.. If I can do it for a one column table I can do it for my table.
当您想要使用变量时,需要在TOP
子句之后使用parens:
UPDATE TOP(@N) ...
WITH q AS
(
SELECT TOP (@r) *
FROM mytable
ORDER BY
col1
)
UPDATE q
SET co12 = @value
UPDATE TOP (@r)
will work but it will update any @r
rows in no particular order .
From the documentation :
The rows referenced in the
TOP
expression used withINSERT
,UPDATE
, orDELETE
are not arranged in any order.TOP n
returnsn
random rows.
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.