我的问题并不是太复杂,但我是PL / SQL的新手。
我需要根据某些条件从COMPANIES表中进行选择。然后我需要遍历这些并将一些字段转换为不同的格式(我为此创建了函数),最后使用此转换后的版本连接到引用表以获取我需要的分数变量。所以基本上:
select id, total_empts, bank from COMPANIES where turnover > 100000
循环选择
insert into MY_TABLE (select score from REF where conversion_func(MY_CURSOR.total_emps) = REF.total_emps)
这基本上就是我要做的。它稍微复杂一点,但我只是在寻找基础知识,以及如何处理它让我入门!
答案 0 :(得分:13)
以下是PL / SQL中游标循环的基本语法:
BEGIN
FOR r_company IN (
SELECT
ID,
total_emps,
bank
FROM
companies
WHERE
turnover > 100000
) LOOP
INSERT INTO
my_table
SELECT
score
FROM
ref_table
WHERE
ref.total_emps = conversion_func( r_company.total_emps )
;
END LOOP;
END;
/
答案 1 :(得分:3)
您无需使用PL / SQL执行此操作:
insert into my_table
select score
from ref r
join companies c
on r.total_emps on conversion_func(c.total_emps)
where c.turnover > 100000
如果您必须按照要求在PL / SQL循环中执行此操作,那么我将确保您尽可能少地完成工作。但是,我建议bulk collect而不是循环。
begin
for xx in ( select conversion_func(total_emps) as tot_emp
from companies
where turnover > 100000 ) loop
insert into my_table
select score
from ref
where total_emps = xx.tot_emp
;
end loop;
end;
/
对于任何一种方法,您需要ref.total_emps
上的一个索引,最好是companies.turnover