0

我是 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

现在它被认为是单一值....

4

2 回答 2

0

我使用过程 dynamic_query_build 创建了匿名块。在那里添加了代码,它将 VARCHAR2 变量拆分为 vararray。我认为,您问题的关键是这一行 - 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;
/
于 2013-07-30T14:03:59.200 回答
0

在 dbms_output 语句中进行替换,这将消除字符串中的引号

     ....
     dbms_output.put_line replace (replace (plan_sku_id(i), '''' ))',',chr(10);
     .....
于 2013-07-30T12:56:03.237 回答