更新查询 - Oracle

nam*_*ked 2 sql oracle oracle11g

我无法理解以下 SQL 查询的问题。我试图将 ABC 列从表 TABLE3 复制到 TABLE1,其中 TABLE2 具有两者之间的公共列。

UPDATE TABLE1 CS  
  SET CS.ABC = TC.ABC  
WHERE CS.COMMON_COLUMN = (  
    SELECT CGL.COMMON_COLUMN 
    FROM TABLE2 CGL, 
         TABLE3 TC  
    WHERE   CGL.PRD_ID = TC.PRD_ID  
    AND  CGL.PRD_VER = TC.PRD_VER  
    AND  CGL.PY_ID = TC.PY_ID  
    AND  CGL.TPY_ID = TC.TPY_ID  
)
Run Code Online (Sandbox Code Playgroud)

我遇到了错误:

SQL 错误: ORA-00904: "TC"."ABC": 无效标识符 00904. 00000 - "%s: invalid identifier" *Cause:
*Action:

[编辑; 请阅读下面的解释]
所以我更新了查询,让我的解释更有意义。Table1Table2由4列连接PRD_ID, PRD_VER, PY_ID and TPY_ID。此组合在 Table2 中找到多行,因为它不是唯一/主键组合。对于从 Table2 检索的每一行,该列common_column是更新 Table3 所需的,因为common_column它只与一行相关联。

例子。

表格1

 PRD_ID, PRD_VER, PY_ID, TPY_ID, COLUMN_USED_FOR_UPDATE
 ------------------------------------------------------  
      1     , 1      , 1    ,1  ,     VALUE1
      2     , 3      , 4    , 5 ,     VALUE2
Run Code Online (Sandbox Code Playgroud)

表2

 PRD_ID,  PRD_VER,  PY_ID,  TPY_ID, COMMON_COLUMN
  ------------------------------------------------
    1  ,     1     , 1,         1,         A
    1  ,     1     , 1,         1,         B
    2,       3     , 4,         5,         C
Run Code Online (Sandbox Code Playgroud)

表3

        COMMON_COLUMN, .... , COLUMN_TO_UPDATE
-------------------------------------------------------
           A,         .....  , null
           B,       ....     , null
           C,          .... ,   null
Run Code Online (Sandbox Code Playgroud)

所以在我执行查询之后,Table3 应该是这样的:

        COMMON_COLUMN, .... , COLUMN_TO_UPDATE
-------------------------------------------------------
           A,         .....  , VALUE1
           B,       ....     , VALUE1
           C,          .... ,   VALUE2
Run Code Online (Sandbox Code Playgroud)

Jus*_*ave 5

我猜你想要

UPDATE table1 cs
   SET cs.abc = (SELECT tc.abc
                   FROM table2 cgl,
                        table3 tc
                  WHERE cgl.prd_id       = tc.prd_id
                    AND cgl.prd_ver      = tc.prd_ver
                    AND cgl.py_id        = tc.py_id
                    AND cgl.typ_id       = tc.tpy_id
                    AND cd.common_column = cgl.common_column)
 WHERE EXISTS (SELECT 1
                 FROM table2 cgl,
                      table3 tc
                WHERE cgl.prd_id       = tc.prd_id
                  AND cgl.prd_ver      = tc.prd_ver
                  AND cgl.py_id        = tc.py_id
                  AND cgl.typ_id       = tc.tpy_id
                  AND cd.common_column = cgl.common_column)
Run Code Online (Sandbox Code Playgroud)

更新:除了对列名和表名的更改之外,我的初始答案似乎适用于您发布的示例数据。请注意,发布 DDL 和 DML 总是更容易,以便我们可以复制您的表和数据,而不是让我们将您的数据转换为 DDL 和 DML。

如果我创建你的表和数据

SQL> create table table1 (
  2    prd_id number,
  3    prd_ver number,
  4    py_id number,
  5    typ_id number,
  6    column_used_for_update varchar2(10)
  7  );

Table created.

SQL> begin
  2    insert into table1 values( 1, 1, 1, 1, 'VALUE1' );
  3    insert into table1 values( 2, 3, 4, 5, 'VALUE2' );
  4  end;
  5  /

PL/SQL procedure successfully completed.

SQL> create table table2 (
  2    prd_id number,
  3    prd_ver number,
  4    py_id number,
  5    typ_id number,
  6    common_column varchar2(10)
  7  );

Table created.

SQL> begin
  2    insert into table2 values( 1, 1, 1, 1, 'A' );
  3    insert into table2 values( 1, 1, 1, 1, 'B' );
  4    insert into table2 values( 2, 3, 4, 5, 'C' );
  5  end;
  6  /

PL/SQL procedure successfully completed.

SQL> create table table3 (
  2    common_column varchar2(10),
  3    column_to_update varchar2(10)
  4  );

Table created.

SQL> begin
  2    insert into table3 values( 'A', null );
  3    insert into table3 values( 'B', null );
  4    insert into table3 values( 'C', null );
  5  end;
  6  /

PL/SQL procedure successfully completed.

SQL> commit;

Commit complete.
Run Code Online (Sandbox Code Playgroud)

然后根据我的初始答案调整表名和列名,看来更新工作正常

SQL> ed
Wrote file afiedt.buf

  1  UPDATE table3 t3
  2     SET t3.column_to_update = (
  3                   SELECT t1.column_used_for_update
  4                     FROM table2 t2,
  5                          table1 t1
  6                    WHERE t1.prd_id        = t2.prd_id
  7                      AND t1.prd_ver       = t2.prd_ver
  8                      AND t1.py_id         = t2.py_id
  9                      AND t1.typ_id        = t2.typ_id
 10                      AND t3.common_column = t2.common_column)
 11   WHERE EXISTS (  SELECT 1
 12                     FROM table2 t2,
 13                          table1 t1
 14                    WHERE t1.prd_id        = t2.prd_id
 15                      AND t1.prd_ver       = t2.prd_ver
 16                      AND t1.py_id         = t2.py_id
 17                      AND t1.typ_id        = t2.typ_id
 18*                     AND t3.common_column = t2.common_column)
SQL> /

3 rows updated.

SQL> select * from table3;

COMMON_COL COLUMN_TO_
---------- ----------
A          VALUE1
B          VALUE1
C          VALUE2
Run Code Online (Sandbox Code Playgroud)