我在处理一些需要循环的任务时遇到了困难。作为一个pl/sql新手,我不会;I don’我不知道该怎么解决那个问题,有人能看看吗?
也许最简单的方法是展示而不是描述我想做的事情:
Select id from table1 t1 join table2 t2 on t1.column=t2.column2
insert into table2 id from previous operation.
Select id from table1 t1 join table2 t2 on t1.column=ts.column2 where t1.id in (Select id from table1 t1 join table2 t2 on t1.column=t2.column2);
insert into table2 id from previous operation
现在,如果之前的操作返回了一些数据,则执行:
Select id from table1 t1 join table2 t2 on t1.column=t2.column2 WHERE t1.column in (Select id from table1 t1 join table2 t2 on t1.column=ts.column2 where t1.id in (Select id from table1 t1 join table2 t2 on t1.column=t2.column2)
insert into tablr2 id from previous operation
依此类推,直到select不会返回任何内容。
当然,我意识到"选择"会在表3中插入重复的ID。我只知道我必须使用循环,但不知道具体是哪一个以及如何使用。有可能处理这个吗?
编辑:
好的,再一次,我有两张桌子:
Table1
Column1
1
2
3
4
5
6
7
8
9
Table2
Column1 Column2 Column3
2345 1 0
5346 2 0
67542 3 23432
3452 4 324665
64356 5 34234
23432 6 0
324665 7 67867
34234 8 0
67867 9 9
Table to insert:
ID
1
2
3
4
作为输入,我得到了ID为的表3,我想插入到例如表4(ID)中。
但其中一些ID可能有一些父/子ID。这是在循环中如何做到这一点的主要困难。通常,我们可以使用以下语句来查找父/子ID:
从表1 t1中选择ID,在t1加入表2 t2。ID=t2.第2列在t2上加入表2 t2。第3列=t2。第1列
而现在基于"表插入"的insert语句(到表4中)应该插入值:1,2,3,4+6和7(正如我们所看到的,6和7具有父id(Column0!=0)。
现在我们想检查账户6,7中是否有与以前相同的父母/子女ID。因此,插入表4应该插入值:9终止
当然,这只是描述2-3次迭代的唯一情况,如果会有更多的父/子ID呢?
我想使用一些表类型,并在每次循环后以某种方式将ID放入表变量中,并将其用于其他迭代。。。
根据您的问题,您似乎希望将所有id插入到表2中,这些id对于基于列"column"的表1和表2都是通用的。把其他没有共同点的行都去掉。
这意味着你希望表2包含表2与表1相交(对于列"column")
现在在sql中,u可以很容易地做到这一点,因为它涉及设置操作,而不是对单行进行操作。
假设你的表1是
id列
1"A"
2"B"
3"C"
表2是
id列
3"A"
4"C"
然后在表2中,你想插入
1——因为"A"在表1和中具有id 1
3——因为"C"在表1 中具有id 3
你可以简单地通过写来做到这一点
insert into table2 (id)
select id from table1 where column in (select column from table2) ;
----我正在根据您的输入编辑答案-------
所以这次我创建了整个表,并尝试复制你提到的每一个场景。
让我们首先创建req表集
create table table2 (row_id number , id number , parent_id number)
CREATE table TABLE1 (id number) ;
CREATE table TABLE3 (id number) ;
CREATE table TABLE4 (id number) ;
现在让我们在这些表中插入相同的值(如示例中提到的u)
--TABLE1 ---
INSERT INTO table1 VALUES
(1);
INSERT INTO table1 VALUES
(2);
INSERT INTO table1 VALUES
(3);
INSERT INTO table1 VALUES
(4);
INSERT INTO table1 VALUES
(5);
INSERT INTO table1 VALUES
(6);
INSERT INTO table1 VALUES
(7);
INSERT INTO table1 VALUES
(8);
INSERT INTO table1 VALUES
(9);
--TABLE2 ---
INSERT INTO table2 VALUES
(2345 , 1 , 0) ;
INSERT INTO table2 VALUES
(5346 , 2 , 0) ;
INSERT INTO table2 VALUES
(67542 , 3 , 23432) ;
INSERT INTO table2 VALUES
(3452 , 4 , 324665);
INSERT INTO table2 VALUES
(64356 , 5 , 34234);
INSERT INTO table2 VALUES
(23432 , 6 , 0);
INSERT INTO table2 VALUES
(324665 , 7 , 67867);
INSERT INTO table2 VALUES
(34234 , 8 , 0);
INSERT INTO table2 VALUES
(
67867 , 9 , 9);
--TABLE3 ---
INSERT INTO table3 VALUES
(1);
INSERT INTO table3 VALUES
(2);
INSERT INTO table3 VALUES
(3);
INSERT INTO table3 VALUES
(4);
set serveroutput on ;
运行这个anoy块现在就可以满足需要了。
DECLARE
table_var VARCHAR2(30) := 'TABLE3' ; -- replace this with any table
lvc_cur sys_refcursor ;
l_num NUMBER ;
cnt number := 0 ;
BEGIN
OPEN lvc_cur FOR 'select distinct id from '||table_var ||' where id in (select table1.id from table1 , table2 where table1.id = table2.id )';
loop
fetch lvc_cur into l_num ;
exit WHEN lvc_cur%notfound ;
INSERT INTO table4(ID)
select id from table2 connect by prior parent_id = row_id start with id = l_num ;
end loop ;
END ;
通过运行以下查询进行检查
select * from table4
ID
----------
1
2
4
7
9
3
6
7 rows selected
使其成为类似insert into .. select from
的
insert into table2(id)
Select id from table1 t1 join table2 t2 on t1.column=t2.column2