我想更新这个绿色框行的Quantity列值200到100 ..怎么做?

时间:2021-09-03 20:46:13

This is tblSales table. It contains two rows with same values.. I want to update this green box row.. please help me.. I'm a beginner to SQL Server.

这是tblSales表。它包含两行具有相同的值..我想更新此绿色框行..请帮助我..我是SQL Server的初学者。

我想更新这个绿色框行的Quantity列值200到100 ..怎么做?

A partial view of the table, from the above image:

表格的局部视图,从上图:

CustID   SalesDate    ItemId    Quantity   Amount
---------------------------------------------------
C1       2009-01-01   ABC       100        1000.00
C2       2009-01-01   ABC       200        2000.00
C4       2009-12-01   PQR       200        2000.00
C4       2009-12-01   PQR       200        2000.00   <---- this one!
C2       2009-01-04   XYZ       500        NULL
C1       2009-01-05   XYZ       NULL       500.00

2 个解决方案

#1


3  

On your object explorer : expand your Database, then expand tables, then right click on your table >and choose 'edit top 200 rows'.

在对象资源管理器上:展开数据库,然后展开表格,然后右键单击表格>并选择“编辑前200行”。

then edit and save it.

然后编辑并保存。

if you want to use query, you could use RN

如果你想使用查询,你可以使用RN

WITH CTE AS(
   SELECT *,
       RN = ROW_NUMBER()OVER(PARTITION BY CustID ORDER BY CustID)
   FROM tblSales
)
UPDATE CTE set Quantity='100' WHERE RN=2 and CustID='C4';

#2


1  

Since I can't see the whole table, I'm not sure if this will work, but . . .

由于我看不到整张桌子,我不确定这是否有用,但是。 。 。

Your records probably have a unique identifier (ID, Primary Key, etc) - and it looks from your image that the duplicated rows have a unique (or at least different) value in the field to the left (partly cut off in the picture). Let's say that the rows have sequential IDs from 1-6. If so, then you can do a simple UPDATE query:

您的记录可能具有唯一标识符(ID,主键等) - 并且从您的图像中查看重复行在左侧字段中具有唯一(或至少不同)值(在图片中部分截断) 。假设这些行具有1-6的顺序ID。如果是这样,那么你可以做一个简单的UPDATE查询:

UPDATE tblSales
SET column_name = new_value, column_name2 = new_value . . .
WHERE ID = 4;

In this case, specifying the changes you want to make - for example, to change the quantity of the fourth row:

在这种情况下,指定要进行的更改 - 例如,更改第四行的数量:

UPDATE tblSales
SET quantity = 100
WHERE ID = 4;

if the ID in that column isn't unique, it's still different from the row above, so you can do:

如果该列中的ID不是唯一的,它仍然与上面的行不同,因此您可以执行以下操作:

UPDATE tblSales
SET quantity = 100
WHERE id = 4 AND CustID = 'C4' and ItemID = 'PQR';

the multiple WHERE will uniquely identify the row in that case (you can run it as a SELECT query first to ensure that it identifies only one row before going ahead with an UPDATE)

在这种情况下,多个WHERE将唯一地标识该行(您可以先将其作为SELECT查询运行,以确保在继续执行UPDATE之前仅标识一行)

if there's NO unique identifier and these are true duplicate rows, I refer you to this: https://support.microsoft.com/en-us/kb/139444

如果没有唯一标识符,并且这些是真正的重复行,我建议你这样:https://support.microsoft.com/en-us/kb/139444

#1


3  

On your object explorer : expand your Database, then expand tables, then right click on your table >and choose 'edit top 200 rows'.

在对象资源管理器上:展开数据库,然后展开表格,然后右键单击表格>并选择“编辑前200行”。

then edit and save it.

然后编辑并保存。

if you want to use query, you could use RN

如果你想使用查询,你可以使用RN

WITH CTE AS(
   SELECT *,
       RN = ROW_NUMBER()OVER(PARTITION BY CustID ORDER BY CustID)
   FROM tblSales
)
UPDATE CTE set Quantity='100' WHERE RN=2 and CustID='C4';

#2


1  

Since I can't see the whole table, I'm not sure if this will work, but . . .

由于我看不到整张桌子,我不确定这是否有用,但是。 。 。

Your records probably have a unique identifier (ID, Primary Key, etc) - and it looks from your image that the duplicated rows have a unique (or at least different) value in the field to the left (partly cut off in the picture). Let's say that the rows have sequential IDs from 1-6. If so, then you can do a simple UPDATE query:

您的记录可能具有唯一标识符(ID,主键等) - 并且从您的图像中查看重复行在左侧字段中具有唯一(或至少不同)值(在图片中部分截断) 。假设这些行具有1-6的顺序ID。如果是这样,那么你可以做一个简单的UPDATE查询:

UPDATE tblSales
SET column_name = new_value, column_name2 = new_value . . .
WHERE ID = 4;

In this case, specifying the changes you want to make - for example, to change the quantity of the fourth row:

在这种情况下,指定要进行的更改 - 例如,更改第四行的数量:

UPDATE tblSales
SET quantity = 100
WHERE ID = 4;

if the ID in that column isn't unique, it's still different from the row above, so you can do:

如果该列中的ID不是唯一的,它仍然与上面的行不同,因此您可以执行以下操作:

UPDATE tblSales
SET quantity = 100
WHERE id = 4 AND CustID = 'C4' and ItemID = 'PQR';

the multiple WHERE will uniquely identify the row in that case (you can run it as a SELECT query first to ensure that it identifies only one row before going ahead with an UPDATE)

在这种情况下,多个WHERE将唯一地标识该行(您可以先将其作为SELECT查询运行,以确保在继续执行UPDATE之前仅标识一行)

if there's NO unique identifier and these are true duplicate rows, I refer you to this: https://support.microsoft.com/en-us/kb/139444

如果没有唯一标识符,并且这些是真正的重复行,我建议你这样:https://support.microsoft.com/en-us/kb/139444