Klapsius Klapsius - 29 days ago 5
SQL Question

SQL Server how to update only one row in database?

How to I can update only one record on db?

Table:

name name1 name2
----------------------------
xx xy xz
xx xx xx
xx xx xx
xx xx xx
xy xx zz


Update query:

UPDATE table1
SET name2 = '01'
WHERE name1='xx'


but I need update only one row per time

s_f s_f
Answer

you can use ROWCOUNT

SET ROWCOUNT 1

UPDATE table1 
SET name2 = '01' 
WHERE name1='xx'

SET ROWCOUNT 0

or you can use update top

UPDATE TOP (1) table1 
SET name2 = '01' 
WHERE name1='xx'
Comments