Oracle while exists(select ...) insert into

问题描述

我正在尝试使用 while 循环将 id 插入到临时表中。 TEMP_TABLE 包含 ID-s,它 = ID_TABLE 中条目的 ID。 ID_TABLE 有两个字段 CON_1 和 CON_2,它们引用 ID_TABLE 中的另一个条目。

我想将 ID_TABLE 中的所有 CON_1 和 CON_2 值添加到 TEMP_TABLE 中,其中 ID_TABLE.ID = TEMP_TABLE.ID 并且 CON_1 或 CON_2 尚未在 TEMP_TABLE 中,然后重复该过程,直到没有剩余的 ID 可以插入(添加 CON_1 之后或 CON_2 到 TEMP_TABLE,这些 ID 可能指的是 ID_TABLE.ID,其中 CON_1 或 CON_2 尚未出现在 TEMP_TABLE 中)。

基本上,一个 ID 可能有连接作为另一个 ID,我想将 ID、它的连接和连接的连接添加到 TEMP_TABLE 中。

到目前为止我所做的查询

begin
    while exists(select extId
                 from (
                          select distinct case
                                              when con.CON_1 = idTable.ID
                                                  then con.CON_2
                                              else con.CON_1
                                              end
                                              as extId
                          from ID_TABLE idTable
                                   inner join TEMP_TABLE temp on idTable.ID = temp.ID
                                   inner join CONNECTIONS_TABLE con on con.CON_2 = idTable.ID
                              or con.CON_1 = idTable.ID)
                 where not exists(select ID from TEMP_TABLE where ID = extId))
        loop
            insert into TEMP_TABLE
            select extId
            from (
                     select distinct case
                                         when con.CON_1 = idTable.ID
                                             then con.CON_2
                                         else con.CON_1
                                         end
                                         as extId
                     from ID_TABLE idTable
                              inner join TEMP_TABLE temp on idTable.id = temp.ID
                              inner join CONNECTIONS_TABLE con on con.CON_2 = idTable.id
                         or con.CON_1 = idTable.ID)
            where not exists(select ID from TEMP_TABLE where ID = extId);
        end loop;
end;

当我运行查询时,出现此错误

pls-00103: Encountered the symbol "INNER" when expecting one of the following:
   ),with group having intersect minus order start union where
   connect

在 Oracle 12c 上运行

解决方法

您似乎试图从 CON_1 递归添加 CON_2CONNECTIONS_TABLE 值,这些值与 ID 中的先前 TEMP_TABLE 值相关联和 ID_TABLE

您似乎不需要 WHILE 循环(甚至 PL/SQL)并且可以使用单个 MERGE 语句和分层查询:

MERGE INTO temp_table DST
USING (
  SELECT DISTINCT
         extid
  FROM   (
    select con_1,con_2
    from   connections_table c
    START WITH EXISTS(
      SELECT 1
      FROM   ID_TABLE i
             inner join TEMP_TABLE t
             on ( i.id = t.ID )
      WHERE  i.id IN ( c.con_1,c.con_2 )
    )
    CONNECT BY NOCYCLE
         PRIOR con_1 IN ( con_1,con_2 )
    OR   PRIOR con_2 IN ( con_1,con_2 )
  )
  UNPIVOT (
    extid FOR con IN ( con_1 AS 1,con_2 AS 2 )
  )
) src
ON ( src.extID = dst.id )
WHEN NOT MATCHED THEN
  INSERT ( id ) VALUES ( src.extid );

初始设置:

CREATE TABLE temp_table( id ) AS
SELECT 1 FROM DUAL;

CREATE TABLE connections_table( con_1,con_2 ) AS
SELECT 1,2 FROM DUAL UNION ALL
SELECT 3,4 FROM DUAL UNION ALL
SELECT 5,10 FROM DUAL;

CREATE TABLE id_table ( id ) AS
SELECT LEVEL FROM DUAL CONNECT BY LEVEL <= 10;

将插入 3 行然后:

SELECT * FROM temp_table;

输出:

ID
1
2
4
3

dbfiddle here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...