Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I update n rows in a table?

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.

like image 731
Moose Avatar asked Sep 28 '09 15:09

Moose


Video Answer


1 Answers

You need to use parens after TOP clause when you want to use a variable:

UPDATE TOP(@N) ...
like image 184
mmx Avatar answered Oct 23 '22 07:10

mmx