3

我的数据设置如下:

CREATE TABLE TableA
(
    id int IDENTITY,
    name varchar(256),
    description varchar(256)
)

CREATE TABLE TableB
(
    id int IDENTITY,
    name varchar(256),
    description varchar(256)
) --unique constraint on name, description

CREATE TABLE TableA_TableB
(
    idA int,
    idB int
) --composite key referencing TableA and TableB

情况是我在TableB中有很多重复的记录违反了唯一约束,并且这些重复记录在TableA_TableB中被引用。所以我试图删除那些记录,这很简单(使用以下 CTE),但是更新 TableA_TableB 中的记录以反映这种变化的最佳方法是什么,即让 TableA_TableB 记录引用相同的 ID TableB 而不是每个重复项的不同 ID?

;WITH cte
 AS (SELECT ROW_NUMBER() OVER (PARTITION BY [Name], [Description]
                                   ORDER BY ( SELECT 0)) RN
     FROM   TableB)

DELETE FROM cte
WHERE  RN = 1
4

1 回答 1

2

b.RowNum=1改为b.RowNum>1

首先,您应该尝试使用ROLLBACK,然后,如果可以,取消注释COMMIT此脚本未经测试):

DECLARE @UpdatedRows TABLE(ID INT PRIMARY KEY);

BEGIN TRANSACTION;

;WITH Base
AS(
    SELECT  ROW_NUMBER() OVER (PARTITION BY [Name], [Description] ORDER BY ( SELECT 0)) RowNum,
        MIN(id) OVER(PARTITION BY [Name], [Description]) AS NewID,
        ID -- Old ID
    FROM    TableB
),TableB_RowsForUpdate
AS(
    SELECT  *
    FROM    Base b
    WHERE   b.RowNum>1
)
UPDATE  target
SET IDB=b.NewID
OUTPUT  deleted.IDB INTO @UpdatedRows
FROM    TableA_TableB target
INNER JOIN TableB_RowsForUpdate b ON target.IDB=b.ID;

DELETE  b
FROM    TableB b INNER JOIN @UpdatedRows upd ON b.ID=upd.ID;

ROLLBACK;
-- COMMIT;
于 2013-07-22T15:59:48.567 回答