如何将查询输出迭代到另一个选择查询作为输入参数?

时间:2015-06-26 05:35:29

标签: oracle plsql oracle11g oracle-sqldeveloper plsqldeveloper

我想将以下查询的输出作为输入参数传递给另一个查询。

 SELECT DISTINCT MV_sum.exch
          FROM LLDMA.MERCAUDIT_VOLUME_AGG_SUMMARY MV_sum,
               EXCHANGE_MAPPING ex_map
         WHERE MV_SUM.EXCH = ex_map.agora_exchange
ORDER BY 1;

query1的示例OUTPUT:

ABC
CDE
DEF
GHI
XYX

我想将此输出1作为输入参数传递给下面的查询 将输出迭代到另一个查询

我编写了下面的代码,因为我是oracle的新手,但它抛出了一些错误 请帮助我......

DECLARE
   exch_name       VARCHAR2 (200);
   v_exch          VARCHAR2 (1000);
   v_exch_output   VARCHAR2 (4000);
   v_cnt_tot       VARCHAR2 (4000);
   abc             VARCHAR2 (4000);
   v_str           VARCHAR2 (4000);

   CURSOR tbl
   IS
        SELECT DISTINCT MV_sum.exch
          FROM LLDMA.MERCAUDIT_VOLUME_AGG_SUMMARY MV_sum,
               EXCHANGE_MAPPING ex_map
         WHERE MV_SUM.EXCH = ex_map.agora_exchange
      ORDER BY 1;
BEGIN
   v_cnt_tot := 0;

   OPEN tbl;

   LOOP
      FETCH tbl INTO exch_name;

      EXIT WHEN tbl%NOTFOUND;
      v_str :=
         ' select distinct ''ICTO-10510'' ,   MV_sum.exch , to_char(to_date(MV_sum.TRADEDATE,''yymmdd''), ''dd-Mon-yyyy'')   ,  ''   ,  ''TDV''   ,  MV_sum.new  ,   ''NEW'' ,  '' , ''-- into abc  
from LLDMA.MERCAUDIT_VOLUME_AGG_SUMMARY MV_sum , EXCHANGE_MAPPING ex_map
where (TRADEDATE between TO_CHAR  (sysdate-545, ''YYMMDD'') and TO_CHAR  (sysdate, ''YYMMDD'') )
and MV_SUM.EXCH = ex_map.agora_exchange
and MV_sum.exch = exch_name
group by MV_sum.exch,
to_char(to_date(MV_sum.TRADEDATE, ''yymmdd''), ''dd-Mon-yyyy''),
MV_sum.new';

      EXECUTE IMMEDIATE v_str;

      DBMS_OUTPUT.PUT_LINE (v_str);
   END LOOP;



   CLOSE tbl;
END;
/

请让我知道如何做到这一点。

3 个答案:

答案 0 :(得分:0)

代码不会将结果作为" select语句"如果这是你需要实现的目标。 从你的代码我注意到: - 您仅使用游标迭代EXCHANGE_MAPPING表中的不同值 - 你可以取出这个语句EXECUTE IMMEDIATE v_str - 如果仅使用select语句,则将获得相同的结果

    select distinct 'ICTO-10510' ,   MV_sum.exch ,    to_char(to_date(MV_sum.TRADEDATE,'yymmdd'), 'dd-Mon-yyyy'),
                '','TDV',  MV_sum.new ,  'NEW' ,  '', ''
from LLDMA.MERCAUDIT_VOLUME_AGG_SUMMARY MV_sum , EXCHANGE_MAPPING ex_map
where (TRADEDATE between TO_CHAR  (sysdate-545, 'YYMMDD') and TO_CHAR  (sysdate, 'YYMMDD') )
and MV_SUM.EXCH = ex_map.agora_exchange
group by MV_sum.exch,
to_char(to_date(MV_sum.TRADEDATE, 'yymmdd'), 'dd-Mon-yyyy'), MV_sum.new

答案 1 :(得分:0)

您好,您可以尝试以下代码。

DECLARE
  exch_name     VARCHAR2(200);
  v_exch        VARCHAR2(1000);
  v_exch_output VARCHAR2(4000);
  v_cnt_tot     VARCHAR2(4000);
  abc           VARCHAR2(4000);
  v_str         VARCHAR2(4000);

  CURSOR tbl IS
    SELECT DISTINCT MV_sum.exch
      FROM LLDMA.MERCAUDIT_VOLUME_AGG_SUMMARY MV_sum, EXCHANGE_MAPPING ex_map
     WHERE MV_SUM.EXCH = ex_map.agora_exchange
     ORDER BY 1;
BEGIN
  v_cnt_tot := 0;
  for x in tbl loop
    -- no need of EXECUTE IMMEDIATE it will harm performance
    select distinct '' ICTO - 10510 '',
                    MV_sum.exch,
                    to_char(to_date(MV_sum.TRADEDATE, '' yymmdd ''), '' dd - Mon - yyyy ''),
                    '',
                    '' TDV '',
                    MV_sum.new,
                    '' NEW '',
                    '',
                    '' -- into abc  
      from LLDMA.MERCAUDIT_VOLUME_AGG_SUMMARY MV_sum, EXCHANGE_MAPPING ex_map
     where (TRADEDATE between TO_CHAR(sysdate - 545, '' YYMMDD '') and TO_CHAR(sysdate, '' YYMMDD ''))
       and MV_SUM.EXCH = ex_map.agora_exchange
       and MV_sum.exch = exch_name
    **--and  MV_sum.exch = x.exch**  exch value from upper query  
     group by MV_sum.exch, to_char(to_date(MV_sum.TRADEDATE, '' yymmdd ''), '' dd - Mon - yyyy ''), MV_sum.new;

  end loop;

exception
  when others then
    null;
END;

答案 2 :(得分:0)

嗨,你可以尝试这个你需要通过rownum = 1限制数量或行数如果你对所有行感兴趣那么你需要使用集合来保存所有值。如果重要,那么你不知道订单对你有什么影响,那么你需要在循环中修改查询

DECLARE
  exch_name VARCHAR2(200);
  v_exch    VARCHAR2(1000);
  output    VARCHAR2(4000);
  abc       VARCHAR2(4000); -- remove this.

  CURSOR tbl IS
    SELECT DISTINCT MV_sum.exch
      into exch_name
      FROM LLDMA.MERCAUDIT_VOLUME_AGG_SUMMARY MV_sum, EXCHANGE_MAPPING ex_map
     WHERE MV_SUM.EXCH = ex_map.agora_exchange
     ORDER BY 1;
BEGIN

  for x in tbl loop
    select 'ICTO-10510' || ',' || MV_sum.exch || ',' || to_char(to_date(MV_sum.TRADEDATE, 'yymmdd'), 'dd-Mon-yyyy') || ',' || '' || ',' || 'TDV' || ',' ||
           MV_sum.new || ',' || "NEW" || ',' || '' || ',' || ''
      into output
      from LLDMA.MERCAUDIT_VOLUME_AGG_SUMMARY MV_sum, EXCHANGE_MAPPING ex_map
     where (TRADEDATE between TO_CHAR(sysdate - 545, 'YYMMDD') and TO_CHAR(sysdate, 'YYMMDD'))
       and MV_SUM.EXCH = ex_map.agora_exchange
       and MV_sum.exch = x.exch --**changes**
       and rownum = 1
     group by MV_sum.exch, to_char(to_date(MV_sum.TRADEDATE, 'yymmdd'), 'dd-Mon-yyyy'), MV_sum.new;

    /* EXECUTE IMMEDIATE abc
    into output; */
    --dont need this .. .. 
    DBMS_OUTPUT.PUT_LINE(output);
  end loop;

exception
  when others then
    DBMS_OUTPUT.PUT_LINE('null');
    --null;`enter code here`
END;