甲骨文 while exists(select ...) insert into
Oracle while exists(select ...) insert into
我正在尝试使用 while 循环将 id 插入临时 table。
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-s 可能指的是 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
运行 甲骨文 12c
您似乎试图递归地添加 CONNECTIONS_TABLE
中的 CON_1
或 CON_2
值,这些值连接到 [=17] 中的先前 ID
值=] 和 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, 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
db<>fiddle here
我正在尝试使用 while 循环将 id 插入临时 table。 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-s 可能指的是 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
运行 甲骨文 12c
您似乎试图递归地添加 CONNECTIONS_TABLE
中的 CON_1
或 CON_2
值,这些值连接到 [=17] 中的先前 ID
值=] 和 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, 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
db<>fiddle here