我试图创建一个将2个集合相互比较的函数。让我先解释一下我要先做的事情:我有一张账单表和一张老师桌。我想使用此功能查看是否有任何帐户没有链接到教师并删除它们。我已完成大部分功能,但我似乎无法弄清楚如何删除未从数据库链接的帐户。有没有人有任何想法?这是我的代码:
declare
type type_coll_accnr is table of account.account_id%type;
type type_coll_teachernr is table of teacher.teacher_id%type;
t_teachernr type_coll_teachernr;
t_accnr type_coll_accnr;
begin
select account_account_id
bulk collect into t_teachernr
from teacher;
select account_id
bulk collect into t_accnr
from account
where acces = 'Teacher';
for i_counter IN 1 .. t_teachernr.count
loop
if t_teachernr(i_counter) member of t_accnr then
dbms_output.put_line(t_accnr(i_counter));
else
delete from account where account_id = t_accnr(i_counter);
-- It should delete the account here, but I have no clue how.
end if;
end loop;
end;
我不得不翻译所有这些,所以如果我错过了什么,请告诉我。我对PL / SQL也很陌生,所以我知道这可能看起来像个愚蠢的问题!
答案 0 :(得分:1)
好的,我是对的 - 循环应该在t_accnr列表中。它应该检查来自t_accnr的当前值是否是t_teachernr的成员。 我创建表并检查它。 t_accnr很大,但不是问题
declare
type type_coll_accnr is table of account.account_id%type;
type type_coll_teachernr is table of teacher.teacher_id%type;
t_teachernr type_coll_teachernr;
t_accnr type_coll_accnr;
begin
select account_account_id
bulk collect into t_teachernr
from teacher;
select account_id
bulk collect into t_accnr
from account where acces = 'Teacher';
for i_counter IN 1 .. t_accnr.count
loop
if t_accnr(i_counter) member of t_teachernr then
dbms_output.put_line(t_accnr(i_counter));
else
dbms_output.put_line('delete from account where account_id ='|| t_accnr(i_counter));
delete from account where account_id = t_accnr(i_counter);
-- It should delete the account here, but I have no clue how.
end if;
end loop;
end;
希望有所帮助