For example, TOP 3 based on the key, col1:
CREATE TABLE tbl (col1 INT PRIMARY KEY, col2 INT NOT NULL);
INSERT INTO tbl (col1, col2)
SELECT 1,0 UNION ALL
SELECT 2,0 UNION ALL
SELECT 3,0 UNION ALL
SELECT 4,0 UNION ALL
SELECT 5,0 ;
DECLARE @n INT;
SET @n = 3;
SET col2 = 1
WHERE @n >
FROM tbl AS T
WHERE T.col1 < tbl.col1);
It is also worth noting that the UPDATE TOP feature in SQL Server 2005
does NOT solve the same problem. UPDATE TOP (n) will update a RANDOM
selection of n rows so it isn't suitable for performing updates in any
predetermined order. 2005's RANK() function does help however:
WITH t AS
(SELECT RANK() OVER (ORDER BY col1) AS rnk,
SET col2 = 2
WHERE rnk <= @n;
Hope this helps.
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online: