如何在程序中从IN参数中获取VARRAY的值

时间:2013-07-30 12:44:50

标签: sql oracle plsql oracle10g plsqldeveloper

我是PL / SQL的新手......在Varray中如何从IN参数中获取多个值...否则是否有其他方法可以获取值...

我想通过VArray来交流价值...如果有任何其他选择那么它的罚款.. 编码:

CREATE OR REPLACE
PROCEDURE dynamic_query_build(
    vr_plan_sku_id IN VARCHAR2 )
IS
type plan_sku_id_array IS VARRAY(999) OF VARCHAR2(5000);
plan_sku_id plan_sku_id_array;
total           INTEGER;
vrx_plan_sku_id VARCHAR2(3000);
BEGIN
  vrx_plan_sku_id:= REPLACE(vr_plan_sku_id,',',chr(39)||','||chr(39));
  plan_sku_id    := plan_sku_id_array(chr(39)||vrx_plan_sku_id||chr(39));
  total          := plan_sku_id.count;
  FOR i          IN 1 .. total
  LOOP
    dbms_output.put_line(plan_sku_id(i));
  END LOOP;
EXCEPTION
WHEN OTHERS THEN
  raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
END dynamic_query_build;

执行:

set serveroutput on;
declare
vr_plan_sku_id varchar2(200) := '5863314,5863315';
BEGIN
   dynamic_query_build(vr_plan_sku_id);
END;
/

我的输出:

anonymous block completed
'5863314','5863315'

预期产出:

5863314
5863315

现在它正在考虑作为单一价值......

2 个答案:

答案 0 :(得分:0)

在dbms_output语句中放置一个替换,这将消除字符串

中的引号
     ....
     dbms_output.put_line replace (replace (plan_sku_id(i), '''' ))',',chr(10);
     .....

答案 1 :(得分:0)

我用过程dynamic_query_build创建了一个匿名块。添加了代码,将VARCHAR2变量拆分为varray。 我想,你的问题的关键是这一行 - plan_sku_id.EXTEND(); 您可以动态扩展varray,但只能达到定义的最大值(在您的情况下为999)。

DECLARE
vr_plan_sku_id varchar2(200) := '5863314,5863315';
PROCEDURE dynamic_query_build(
    vr_plan_sku_id IN VARCHAR2 )
IS
type plan_sku_id_array IS VARRAY(999) OF VARCHAR2(5000);
plan_sku_id plan_sku_id_array;
total           INTEGER;

position PLS_INTEGER := 0;
last_position PLS_INTEGER := 1;
tmp VARCHAR2(5000);
counter PLS_INTEGER := 1;
BEGIN
  plan_sku_id := plan_sku_id_array();
  LOOP
    position := INSTR(vr_plan_sku_id, ',', last_position);
    IF position > 0 THEN
      tmp := SUBSTR(vr_plan_sku_id, last_position, position - last_position);
      last_position := position + 1;
    ELSE
      tmp := SUBSTR(vr_plan_sku_id, last_position);
    END IF;
    plan_sku_id.EXTEND();
    plan_sku_id(counter) := tmp;
    counter := counter + 1;
    EXIT WHEN position = 0 OR counter > 10;
  END LOOP;
  total          := plan_sku_id.count;
  FOR i          IN 1 .. total
  LOOP
    dbms_output.put_line(plan_sku_id(i));
  END LOOP;
EXCEPTION
WHEN OTHERS THEN
  raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
END dynamic_query_build;
BEGIN
   dynamic_query_build(vr_plan_sku_id);
END;
/