2

我正在尝试为 SQL Server 2008 重写这个 Oracle 查询:

UPDATE "LOAD"."UNITS_OF_MEASURE" 
SET ("UOM_DESC", "UOM_SYMBOL", "UOM_MUL_FACTOR", "UOM_ADD_FACTOR", "UOM_DEC_PLACE", "UOM_REFERENCE", "UOM_SUBTYPE_ID") =
                 ( SELECT "UOM_DESC", "UOM_SYMBOL", "UOM_MUL_FACTOR", "UOM_ADD_FACTOR", "UOM_DEC_PLACE", 0 AS "UOM_REFERENCE", NULL AS "UOM_SUBTYPE_ID"
                    FROM ( SELECT DISTINCT "UOM_TYPE_ID", "UOM_CODE", "UOM_DESC", "UOM_SYMBOL", "UOM_MUL_FACTOR", "UOM_ADD_FACTOR", "UOM_DEC_PLACE"
                            FROM "LOAD"."LOADER_UNITS_OF_MEASURE"
                            INNER JOIN "LOAD"."UOM_TYPES" ON ("UOM_TYPES"."UOM_TYPE_CODE"="LOADER_UNITS_OF_MEASURE"."UOM_TYPE_CODE")
                         ) SELECTION
                    WHERE ("SELECTION"."UOM_TYPE_ID"="UNITS_OF_MEASURE"."UOM_TYPE_ID")
                    AND ("SELECTION"."UOM_CODE"="UNITS_OF_MEASURE"."UOM_CODE")
                )
WHERE EXISTS  ( SELECT "UOM_DESC", "UOM_SYMBOL", "UOM_MUL_FACTOR", "UOM_ADD_FACTOR", "UOM_DEC_PLACE", 0 AS "UOM_REFERENCE", NULL AS "UOM_SUBTYPE_ID"
                  FROM ( SELECT DISTINCT "UOM_TYPE_ID", "UOM_CODE", "UOM_DESC", "UOM_SYMBOL", "UOM_MUL_FACTOR", "UOM_ADD_FACTOR", "UOM_DEC_PLACE"
                          FROM "LOAD"."LOADER_UNITS_OF_MEASURE"
                          INNER JOIN "LOAD"."UOM_TYPES" ON ("UOM_TYPES"."UOM_TYPE_CODE"="LOADER_UNITS_OF_MEASURE"."UOM_TYPE_CODE")
                        ) SELECTION
                 WHERE ("SELECTION"."UOM_TYPE_ID"="UNITS_OF_MEASURE"."UOM_TYPE_ID")
                 AND ("SELECTION"."UOM_CODE"="UNITS_OF_MEASURE"."UOM_CODE")
)

内部SELECT查询应该保持不变,因为查询是由独立于 DBMS 的查询构建器层自动组成的。

除其他外,我尝试了以下语法:

UPDATE "LOAD"."UNITS_OF_MEASURE" 
SET "UOM_DESC" = i."UOM_DESC", "UOM_SYMBOL" = i."UOM_SYMBOL", "UOM_MUL_FACTOR" = i."UOM_MUL_FACTOR", "UOM_ADD_FACTOR" = i."UOM_ADD_FACTOR", "UOM_DEC_PLACE" = i."UOM_DEC_PLACE",
     "UOM_REFERENCE" = 0, "UOM_SUBTYPE_ID" = NULL FROM
                 ( SELECT "UOM_DESC", "UOM_SYMBOL", "UOM_MUL_FACTOR", "UOM_ADD_FACTOR", "UOM_DEC_PLACE", 0 AS "UOM_REFERENCE", NULL AS "UOM_SUBTYPE_ID"
                    FROM ( SELECT DISTINCT "UOM_TYPE_ID", "UOM_CODE", "UOM_DESC", "UOM_SYMBOL", "UOM_MUL_FACTOR", "UOM_ADD_FACTOR", "UOM_DEC_PLACE"
                            FROM "LOAD"."LOADER_UNITS_OF_MEASURE"
                            INNER JOIN "LOAD"."UOM_TYPES" ON ("UOM_TYPES"."UOM_TYPE_CODE"="LOADER_UNITS_OF_MEASURE"."UOM_TYPE_CODE")
                         ) SELECTION
                    WHERE ("SELECTION"."UOM_TYPE_ID"="UNITS_OF_MEASURE"."UOM_TYPE_ID")
                    AND ("SELECTION"."UOM_CODE"="UNITS_OF_MEASURE"."UOM_CODE")
                )i
WHERE EXISTS ( SELECT "UOM_DESC", "UOM_SYMBOL", "UOM_MUL_FACTOR", "UOM_ADD_FACTOR", "UOM_DEC_PLACE", 0 AS "UOM_REFERENCE", NULL AS "UOM_SUBTYPE_ID"
                  FROM ( SELECT DISTINCT "UOM_TYPE_ID", "UOM_CODE", "UOM_DESC", "UOM_SYMBOL", "UOM_MUL_FACTOR", "UOM_ADD_FACTOR", "UOM_DEC_PLACE"
                          FROM "LOAD"."LOADER_UNITS_OF_MEASURE"
                          INNER JOIN "LOAD"."UOM_TYPES" ON ("UOM_TYPES"."UOM_TYPE_CODE"="LOADER_UNITS_OF_MEASURE"."UOM_TYPE_CODE")
                        ) SELECTION
                 WHERE ("SELECTION"."UOM_TYPE_ID"="UNITS_OF_MEASURE"."UOM_TYPE_ID")
                 AND ("SELECTION"."UOM_CODE"="UNITS_OF_MEASURE"."UOM_CODE")
)

但不幸的是"UNITS_OF_MEASURE"."UOM_TYPE_ID""UNITS_OF_MEASURE"."UOM_CODE"第 9-10 行无法解决。

4

1 回答 1

3

基本上,这看起来好像您将派生表与外部表(UPDATE 的目标)相关联,而不是将其连接到后者。这在 Transact-SQL 中不起作用(事实上,我也怀疑这是否适用于 Oracle)。

但是,您可以使用以下UPDATE...FROM语法:只需摆脱中间层 SELECT 并使用最内层的 SELECT 作为派生表来加入UNITS_OF_MEASURE,即像这样:

UPDATE "LOAD"."UNITS_OF_MEASURE" 
SET "UOM_DESC" = "SELECTION"."UOM_DESC",
    "UOM_SYMBOL" = "SELECTION"."UOM_SYMBOL",
    "UOM_MUL_FACTOR" = "SELECTION"."UOM_MUL_FACTOR",
    "UOM_ADD_FACTOR" = "SELECTION"."UOM_ADD_FACTOR",
    "UOM_DEC_PLACE" = "SELECTION"."UOM_DEC_PLACE",
    "UOM_REFERENCE" = 0,
    "UOM_SUBTYPE_ID" = NULL
FROM ( SELECT DISTINCT "UOM_TYPE_ID", "UOM_CODE", "UOM_DESC", "UOM_SYMBOL",
                       "UOM_MUL_FACTOR", "UOM_ADD_FACTOR", "UOM_DEC_PLACE"
       FROM "LOAD"."LOADER_UNITS_OF_MEASURE"
       INNER JOIN "LOAD"."UOM_TYPES" ON ("UOM_TYPES"."UOM_TYPE_CODE"="LOADER_UNITS_OF_MEASURE"."UOM_TYPE_CODE")
     ) SELECTION
WHERE ("SELECTION"."UOM_TYPE_ID"="UNITS_OF_MEASURE"."UOM_TYPE_ID")
  AND ("SELECTION"."UOM_CODE"="UNITS_OF_MEASURE"."UOM_CODE")
;

这会将SELECTION派生表连接到"LOAD"."UNITS_OF_MEASURE"并使用派生表的数据来更新另一个表的列。

于 2013-05-27T13:28:33.440 回答