使用t-sql更新一行

时间:2022-09-26 21:36:35

I want to update a row in my date base. The problem is, through a mistake on my part, I have two identical rows of data. How do I run the update on just one row?

我想更新我的数据库中的一行。问题是,由于我的错误,我有两行相同的数据。如何只对一行运行更新?

5 个解决方案

#1


12  

Often tables have a unique ID. And you should filter on that.

表通常有唯一的ID,您应该对其进行过滤。

For example,

例如,

UPDATE YourTable
SET YourColumnToUpdate = 'your_value'
WHERE YourUniqueColumn = @Id

If your table does not have a unique ID, consider adding one: an integer column with a Primary Key and Identity.

如果您的表没有唯一的ID,请考虑添加一个:具有主键和标识的整数列。

#2


21  

In SQL Server 2005+, you can use

在SQL Server 2005+中,您可以使用

UPDATE TOP (1) ....

The advantage of this over SET ROWCOUNT is that any triggers will not be subject to a ROWCOUNT limit, which is almost certainly a good thing.

这种优于SET ROWCOUNT的优点是,任何触发器都不受ROWCOUNT限制,这几乎肯定是一件好事。

#3


3  

I suggest you go back and normalize your database. At the very least add a auto increment int primary key column and use that id. Using UPDATE TOP 1 might work and directly answers your question, but non - normalization of your database is the "real" problem.

我建议您返回并规范化数据库。至少添加一个自动增量int主键列并使用该id。

http://en.wikipedia.org/wiki/Database_normalization

http://en.wikipedia.org/wiki/Database_normalization

#4


0  

Use SET ROWCOUNT;

使用设置ROWCOUNT;

SET ROWCOUNT 1; UPDATE Production.ProductInventory SET Quantity = 400 WHERE Quantity < 300; GO

设置ROWCOUNT 1;更新生产。产品库存数量= 400,数量< 300;去

http://msdn.microsoft.com/en-us/library/ms188774.aspx

http://msdn.microsoft.com/en-us/library/ms188774.aspx

#5


0  

-- 01. create the table for the test-data
CREATE TABLE dbo.Orders (
    ID INTEGER
    ,Value DECIMAL(10, 2)
    ,Descr NVARCHAR(100)
    ,BookingDate DATETIME
    ,UserName NVARCHAR(100)
    ,CountMeasure INTEGER
)

-- 02. save the timestamp for the inserts
DECLARE @date AS DATETIME
SET @date = getdate()

-- 03. inserting test-data
INSERT INTO dbo.Orders VALUES (1,499.99,'Notebook',@date,'tgr',0)
INSERT INTO dbo.Orders VALUES (2,650.00,'PC',@date,'tgr',0)
INSERT INTO dbo.Orders VALUES (3,29.50,'Keyboard',@date,'tgr',0)

-- 04. adding the duplicate entry
INSERT INTO dbo.Orders VALUES (2,650.00,'PC',@date,'tgr',0)

-- 05. viewing the 4 Rows
SELECT * FROM dbo.Orders

-- 06. viewing the distinct 3 Rows
SELECT DISTINCT * FROM dbo.Orders

/* You don't want to delete the duplicate row, but you want to count 
   the distinct IDs using SUM on the CountMeasure-Column */

-- 07. alternativ solution (which may does not fit your requirements)
/* So your result shoud be the same like this */
SELECT COUNT(DISTINCT ID) as DistinctCount
FROM dbo.Orders

-- 08. Understanding the solution
/* To understand the solution we take a look on the main-part 
   We generate for each ID a Row-Number ordered by random  
   Details: https://msdn.microsoft.com/de-de/library/ms186734%28v=sql.120%29.aspx */
SELECT * , ROW_NUMBER() OVER (PARTITION BY ID ORDER BY NEWID()) AS RowNumberForEachId
FROM dbo.Orders

-- 09. The update statement
/* We use this part to update our table */
UPDATE a
SET CountMeasure = 1
FROM (-- Orders incl
      SELECT * , ROW_NUMBER() OVER (PARTITION BY ID ORDER BY NEWID()) as rowNum
      FROM dbo.Orders
) as a
WHERE rowNum = 1

-- 10. Viewing the Result 
SELECT * FROM dbo.Orders 

-- 11. Comparing Count(DISTINCT ...) with the SUM(...) alternative
SELECT COUNT(DISTINCT ID) as countDistinct, SUM(CountMeasure) as sumCountMeasure
FROM dbo.Orders

-- 12. Removing the test-table
DROP TABLE dbo.Orders

#1


12  

Often tables have a unique ID. And you should filter on that.

表通常有唯一的ID,您应该对其进行过滤。

For example,

例如,

UPDATE YourTable
SET YourColumnToUpdate = 'your_value'
WHERE YourUniqueColumn = @Id

If your table does not have a unique ID, consider adding one: an integer column with a Primary Key and Identity.

如果您的表没有唯一的ID,请考虑添加一个:具有主键和标识的整数列。

#2


21  

In SQL Server 2005+, you can use

在SQL Server 2005+中,您可以使用

UPDATE TOP (1) ....

The advantage of this over SET ROWCOUNT is that any triggers will not be subject to a ROWCOUNT limit, which is almost certainly a good thing.

这种优于SET ROWCOUNT的优点是,任何触发器都不受ROWCOUNT限制,这几乎肯定是一件好事。

#3


3  

I suggest you go back and normalize your database. At the very least add a auto increment int primary key column and use that id. Using UPDATE TOP 1 might work and directly answers your question, but non - normalization of your database is the "real" problem.

我建议您返回并规范化数据库。至少添加一个自动增量int主键列并使用该id。

http://en.wikipedia.org/wiki/Database_normalization

http://en.wikipedia.org/wiki/Database_normalization

#4


0  

Use SET ROWCOUNT;

使用设置ROWCOUNT;

SET ROWCOUNT 1; UPDATE Production.ProductInventory SET Quantity = 400 WHERE Quantity < 300; GO

设置ROWCOUNT 1;更新生产。产品库存数量= 400,数量< 300;去

http://msdn.microsoft.com/en-us/library/ms188774.aspx

http://msdn.microsoft.com/en-us/library/ms188774.aspx

#5


0  

-- 01. create the table for the test-data
CREATE TABLE dbo.Orders (
    ID INTEGER
    ,Value DECIMAL(10, 2)
    ,Descr NVARCHAR(100)
    ,BookingDate DATETIME
    ,UserName NVARCHAR(100)
    ,CountMeasure INTEGER
)

-- 02. save the timestamp for the inserts
DECLARE @date AS DATETIME
SET @date = getdate()

-- 03. inserting test-data
INSERT INTO dbo.Orders VALUES (1,499.99,'Notebook',@date,'tgr',0)
INSERT INTO dbo.Orders VALUES (2,650.00,'PC',@date,'tgr',0)
INSERT INTO dbo.Orders VALUES (3,29.50,'Keyboard',@date,'tgr',0)

-- 04. adding the duplicate entry
INSERT INTO dbo.Orders VALUES (2,650.00,'PC',@date,'tgr',0)

-- 05. viewing the 4 Rows
SELECT * FROM dbo.Orders

-- 06. viewing the distinct 3 Rows
SELECT DISTINCT * FROM dbo.Orders

/* You don't want to delete the duplicate row, but you want to count 
   the distinct IDs using SUM on the CountMeasure-Column */

-- 07. alternativ solution (which may does not fit your requirements)
/* So your result shoud be the same like this */
SELECT COUNT(DISTINCT ID) as DistinctCount
FROM dbo.Orders

-- 08. Understanding the solution
/* To understand the solution we take a look on the main-part 
   We generate for each ID a Row-Number ordered by random  
   Details: https://msdn.microsoft.com/de-de/library/ms186734%28v=sql.120%29.aspx */
SELECT * , ROW_NUMBER() OVER (PARTITION BY ID ORDER BY NEWID()) AS RowNumberForEachId
FROM dbo.Orders

-- 09. The update statement
/* We use this part to update our table */
UPDATE a
SET CountMeasure = 1
FROM (-- Orders incl
      SELECT * , ROW_NUMBER() OVER (PARTITION BY ID ORDER BY NEWID()) as rowNum
      FROM dbo.Orders
) as a
WHERE rowNum = 1

-- 10. Viewing the Result 
SELECT * FROM dbo.Orders 

-- 11. Comparing Count(DISTINCT ...) with the SUM(...) alternative
SELECT COUNT(DISTINCT ID) as countDistinct, SUM(CountMeasure) as sumCountMeasure
FROM dbo.Orders

-- 12. Removing the test-table
DROP TABLE dbo.Orders