SQL Server如何只更新数据库中的一行?

时间:2021-01-23 23:06:47

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

但我每次只需更新一行

3 个解决方案

#1


35  

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'

#2


-4  

if you want update one row per time, please try to add an Identity Column to your table to identify each row.

如果您希望每次更新一行,请尝试在表中添加标识列以标识每一行。

#3


-4  

You can just add LIMIT 1 at the end of the query.

您只需在查询结尾处添加LIMIT 1即可。

#1


35  

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'

#2


-4  

if you want update one row per time, please try to add an Identity Column to your table to identify each row.

如果您希望每次更新一行,请尝试在表中添加标识列以标识每一行。

#3


-4  

You can just add LIMIT 1 at the end of the query.

您只需在查询结尾处添加LIMIT 1即可。