该过程仅执行以下输出,但是我试图从execute立即执行语句的输出中获取结果。
当前输出:
PL/SQL procedure successfully completed.
select 'PRJA' AS "PRJ_ID", EVENT, email,modified_by,modified from PRJA.TableX UNION ALL
select 'PRJB' AS "PRJ_ID", EVENT, email,modified_by,modified from PRJB.TableX UNION ALL
select 'PRJC' AS "PRJ_ID", EVENT, email,modified_by,modified from PRJC.TableX UNION ALL
select 'PRJD' AS "PRJ_ID", EVENT, email,modified_by,modified from PRJD.TableX;
我期望上面选择/输出的结果:
SET SERVEROUTPUT ON
Declare
TYPE T IS TABLE OF MYTABLE.ID%TYPE INDEX BY PLS_INTEGER;
MYROW T;
v_sql varchar2(500);
v_sql2 varchar2(500);
v_prj_id varchar2(4000):='PRJA,PRJB,PRJC,PRJD';
BEGIN
FOR i IN (SELECT trim(regexp_substr(v_prj_id, '[^,]+', 1, LEVEL)) l
FROM dual
CONNECT BY LEVEL <= regexp_count(v_prj_id, ',') + 1
) LOOP
v_sql := v_sql || 'select '''|| i.l ||''' AS "PRJ_ID", EVENT, email,modified_by,modified from '
|| i.l || '.TableX UNION ALL ' || chr(10) ;
END LOOP;
v_sql2 := RTRIM(v_sql, 'UNION ALL ' || chr(10) ) || ';';
EXECUTE IMMEDIATE v_sql2 BULK COLLECT INTO MYROW;
DBMS_OUTPUT.PUT_LINE(MYROW.XXX);
END;
/
答案 0 :(得分:1)
您的代码有两个明显的问题:
dbms_output
语句将失败,并且put_line()
仅采用标量值。 组装查询的循环也很笨拙。我们可以使用PL / SQL集合使它更整洁。
declare
-- record type to match projection of required output
type r is record (
prj_id varchar2(30)
, event PRJA.TableX.event%type
, email PRJA.TableX.email%type
, modified_by PRJA.TableX.modified_by%type
, modified PRJA.TableX.modified%type
);
TYPE T IS TABLE OF R;
MYROWS T; -- plural because it's a table not a record variable
v_sql varchar2(32767);
-- collection of schemas to query ...
v_prj_ids sys.dbms_debug_vc2coll := sys.dbms_debug_vc2coll('PRJA','PRJB','PRJC','PRJD');
BEGIN
FOR i IN 1 .. v_prj_ids.count() LOOP
if i > 1 then
v_sql := v_sql || chr(10) || ' UNION ALL ' || chr(10)
end if
v_sql := v_sql || 'select '''|| v_prj_ids(i) ||''' AS "PRJ_ID", EVENT, email,modified_by,modified from '
|| v_prj_ids(i) || '.TableX ' ;
END LOOP;
EXECUTE IMMEDIATE v_sql2 BULK COLLECT INTO MYROWS;
DBMS_OUTPUT.PUT_LINE(' number of records =' || MYROWS.count());
END;
/
警告:未经测试,因为我无权访问多用户环境
答案 1 :(得分:0)
您的变量System.Net.Http
的类型为MYROW
,类型为T
,因此,每条记录的数据类型只能与{ {1}}。
在您的代码中,您正在获取TABLE OF MYTABLE.ID%TYPE
(5)列,并尝试将其分配给无法容纳这么多列的MYTABLE.ID
。
如果您只对"PRJ_ID", EVENT, email, modified_by, modified
字段感兴趣,则必须从select子句中删除“ PRJ_ID”以外的所有列。
类似:
MYROW
如果要获取所有数据,则需要创建可以容纳适当属性的对象。有关实现对象表的信息,请参见此forum。
干杯!