SQL Server如何只更新数据库中的一行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26929057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL Server how to update only one row in database?
提问by Klapsius
How to I can update only one record on db?
如何在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
但我每次只需要更新一行
回答by s_f
you can use ROWCOUNT
你可以使用 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'
回答by test30
Please use subquery operating on primary key for better performance
请在主键上使用子查询操作以获得更好的性能
-- INVALID, BUT EXPECTED: update "user" set email = '[email protected]' where email = '[email protected]' limit 1
update "user' set email = '[email protected]' where id = (select id from "user" where email = '[email protected]' limit 1)
回答by Dotnetter
if you want update one row per time, please try to add an Identity Column to your table to identify each row.
如果您想每次更新一行,请尝试在您的表中添加一个标识列以标识每一行。
回答by Thomas
You can just add LIMIT 1 at the end of the query.
您可以在查询的末尾添加 LIMIT 1。