How To Update Only One Row In A Table?
How to I can update only one record in a table? Table: name name1 name2 ---------------------------- xx xy xz xx xx xx xx xx
Solution 1:
you can use ROWCOUNT
SET ROWCOUNT 1UPDATE 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'
Solution 2:
UPDATE table1
SET name2 ='01'WHERE name1='xx'
LIMIT 1;
Solution 3:
Please use subquery operating on primary key for better performance
-- INVALID, BUT EXPECTED: update "user"set email = 'login@com.com'where email = 'login2@com2.com'limit 1
update "user' set email = 'login2@com2.pl' where id = (select id from "user" where email = 'login@com.com' limit 1)
Solution 4:
You can just add LIMIT 1 at the end of the query.
Solution 5:
if you want update one row per time, please try to add an Identity Column to your table to identify each row.
Post a Comment for "How To Update Only One Row In A Table?"