CTE不更新记录的SQL更新

时间:2015-12-31 01:27:32

标签: sql sql-server tsql

我正在尝试更新三个特定列中重复的一组记录。此更新的原因是尝试将此数据插入更新的数据库架构时存在冲突。冲突是由{​​{1}},DM_IDDM_CONTENT_TYPE_ID上添加的新约束引起的。我需要根据行号将DMC_TYPE列调整为1,3或5,以解决此问题。重复数据的样本看起来如此。请注意,前三列是相同的。

DM_CONTENT_TYPE_ID

这是解决约束问题的理想输出:

+--------+--------------------+----------+--------------------------------------+
| DM_ID  | DM_CONTENT_TYPE_ID | DMC_TYPE | DMC_PATH                             |
+--------+--------------------+----------+--------------------------------------+
| 314457 | 1                  | TIF      | \\DOCIMG\CD\1965\19651227\7897-0.tif |
| 314457 | 1                  | TIF      | \\DOCIMG\DR\640\0001_640_0001.tif    |
| 314458 | 1                  | TIF      | \\DOCIMG\CD\1965\19651227\7898-0.tif |
| 314458 | 1                  | TIF      | \\DOCIMG\TD\640\0002_640_0001.tif    |
| 314460 | 1                  | TIF      | \\DOCIMG\CD\1965\19651227\7900-0.tif |
| 314460 | 1                  | TIF      | \\DOCIMG\ZZ\640\0003_640_0003.tif    |
| 314461 | 1                  | TIF      | \\DOCIMG\CD\1965\19651227\7901-0.tif |
| 314461 | 1                  | TIF      | \\DOCIMG\ED\6501\03_0001.tif         |
| 314461 | 1                  | TIF      | \\DOCIMG\ZZ\640\0004_640_0004.tif    |
+--------+--------------------+----------+--------------------------------------+

我开发的脚本是这样的:

+--------+--------------------+----------+--------------------------------------+
| DM_ID  | DM_CONTENT_TYPE_ID | DMC_TYPE | DMC_PATH                             |
+--------+--------------------+----------+--------------------------------------+
| 314457 | 1                  | TIF      | \\DOCIMG\CD\1965\19651227\7897-0.tif |
| 314457 | 3                  | TIF      | \\DOCIMG\DR\640\0001_640_0001.tif    |
| 314458 | 1                  | TIF      | \\DOCIMG\CD\1965\19651227\7898-0.tif |
| 314458 | 3                  | TIF      | \\DOCIMG\TD\640\0002_640_0001.tif    |
| 314460 | 1                  | TIF      | \\DOCIMG\CD\1965\19651227\7900-0.tif |
| 314460 | 3                  | TIF      | \\DOCIMG\ZZ\640\0003_640_0003.tif    |
| 314461 | 1                  | TIF      | \\DOCIMG\CD\1965\19651227\7901-0.tif |
| 314461 | 3                  | TIF      | \\DOCIMG\ED\6501\03_0001.tif         |
| 314461 | 5                  | TIF      | \\DOCIMG\ZZ\640\0004_640_0004.tif    |
+--------+--------------------+----------+--------------------------------------+

现在,当我执行脚本时,它表示相应的行已受到影响。但是,当我检查;WITH CTE AS (SELECT -- Grab the documents that have a duplicate. DM_ID ,DM_CONTENT_TYPE_ID ,DMC_TYPE ,COUNT(*) 'COUNT' FROM [DM_CONTENT] GROUP BY DM_ID ,DM_CONTENT_TYPE_ID ,DMC_TYPE HAVING COUNT(*) > 1), CTE2 AS (SELECT -- Designate the row number for the duplicate documents. DMC.* ,ROW_NUMBER() OVER(PARTITION BY DMC.DM_ID, DMC.DM_CONTENT_TYPE_ID, DMC.DMC_TYPE ORDER BY DMC.DMC_PATH) AS 'ROWNUM' FROM [DM_CONTENT] DMC JOIN CTE ON DMC.DM_ID = CTE.DM_ID), CTE3 AS (SELECT -- Set the new document type ID based on the row number. * ,CASE WHEN ROWNUM = 1 THEN 1 WHEN ROWNUM = 2 THEN 3 WHEN ROWNUM = 3 THEN 5 END AS 'DM_CONTENT_TYPE_ID_NEW' FROM CTE2) UPDATE -- Update the records. DMC SET DMC.DM_CONTENT_TYPE_ID = CTE3.DM_CONTENT_TYPE_ID_NEW FROM [DM_CONTENT] DMC JOIN CTE3 ON DMC.DM_ID = CTE3.DM_ID 表时,[DM_CONTENT]实际上还没有更新,但仍保持DM_CONTENT_TYPE_ID的值。如果1 SELECT CTE3,则DM_CONTENT_TYPE_ID_NEW是适当的新ID。我的逻辑看似合理,但我无法弄清楚我犯的是什么错误。有没有人有任何见解?提前谢谢!

3 个答案:

答案 0 :(得分:1)

尝试

UPDATE CTE3
SET DM_CONTENT_TYPE_ID = DM_CONTENT_TYPE_ID_NEW

而不是您目前正在做的事情。

从CTE更新工作与常规表连接有点不同。

答案 1 :(得分:1)

这写起来似乎要简单得多:

WITH toupdate AS (
      SELECT DMC.*,
            ROW_NUMBER() OVER (PARTITION BY DMC.DM_ID, DMC.DM_CONTENT_TYPE_ID, DMC.DMC_TYPE
                               ORDER BY DMC.DMC_PATH) AS ROWNUM
      FROM DM_CONTENT DMC
     )
UPDATE toupdate
    SET DM_CONTENT_TYPE_ID = (CASE ROWNUM WHEN 2 THEN 3 WHEN 3 THEN 5 END)
    WHERE ROWNUM > 1;

现在,我发现您的join条件仅在DM_ID上有疑问。我认为问题是你在CTE和你的桌子之间得到多个匹配。任意匹配用于更新 - 这恰好是遇到的第一个(因此值为1)。

答案 2 :(得分:1)

任何不可以。重复。试试这种方式

;WITH cte 
     AS (SELECT Row_number() 
                  OVER( 
                    partition BY dm_id, dm_content_type_id, dmc_type 
                    ORDER BY DMC_PATH)     AS Rn, 
                * 
         FROM   dm_content) 
UPDATE cte 
SET    dm_content_type_id = rn + (rn -1)