我是PL / SQL的新手
我有这样的代码
SELECT multiple_string(exclude_acct(5,'ACCOUNT')) ACCT5 INTO v_acct5 from dual;
这个v_acct5,输出是
('0001','0002','0003')
然后我在我的程序中使用这个函数
--This code below is part of my procedure
SELECT multiple_string(exclude_acct(5,'ACCOUNT')) ACCT5 INTO v_acct5 from dual;
select ACCOUNT_NUM
FROM account
where account NOT IN v_acct5
我认为我可以排除这3个帐户,但是当我测试时,我注意到我仍然可以在结果中看到这3个帐户,
如何在PL / SQL中适当地分配变量,以便从结果中排除这3个帐户?
答案 0 :(得分:1)
用作子查询
select ACCOUNT_NUM
FROM account
where account IN(SELECT multiple_string(exclude_acct(5,'ACCOUNT')) ACCT5 INTO
v_acct5 from dual)
答案 1 :(得分:1)
假设你的函数返回了一个数组集合,我试图模拟你的场景。请参阅下文,了解如何取得成果。
创建了一个与您的“帐户”类似的测试表。表 强>
CREATE TABLE TEST_ARRAY
(
A NUMBER,
B VARCHAR2(30 CHAR)
)
插入一些示例记录:
Insert into TEST_ARRAY (A, B) Values (1, '0001');
Insert into TEST_ARRAY (A, B) Values (6, '0002');
Insert into TEST_ARRAY (A, B) Values (7, '0003');
Insert into TEST_ARRAY (A, B) Values (4, 'ABC');
Insert into TEST_ARRAY (A, B) Values (2, 'YRI_1');
Insert into TEST_ARRAY (A, B) Values (4, 'ABC');
创建一个返回varchar2数组的函数
create or replace function ret_strng (x sys.odcivarchar2list) --used oracle defined collection for varchar2.
return sys.odcivarchar2list
as
begin
return x;
end;
程序启动
create or replace procedure disp_func_array_list
as
--- using oracle defined collection for varchar2 to store the result from the function
v_acct5 sys.odcivarchar2list;
cursor cur_Var(v_acct sys.odcivarchar2list) is
select *
from test_array
where B not in ( select column_value from table(v_acct));
begin
--Calling function where my array is getting passed
select ret_strng(sys.odcivarchar2list('0001','0002','0003'))
into v_acct5 --- Storing the value returned by the function
from dual;
for i in cur_Var(v_acct5) -- Passing the resultset(array list) from the function to the Cursor.
loop
dbms_output.put_line(i.a ||'<-->'|| i.b);
end loop;
end;
执行程序:
execute disp_func_array_list;
结果集:
2<-->YRI_1
4<-->ABC
4<-->ABC
19<-->BCG
PL/SQL procedure successfully completed.