如何使用Oracle中另一个select Query的from子句中的select Query中的表名

时间:2012-07-03 21:02:32

标签: oracle select

我需要以下输出

  • 队列名称:队列名称
  • 状态:0个就绪,1个等待,2个已处理,3个已过期 - 队列表
  • Count:计算该状态队列表中的记录数。

实施例

Queue Name         State         Count
---------------------------------------
Email_Q            Processed      5939
Email_response_Q   Waiting         133

我构造了以下SQL

SELECT 'select ''' || owner || '.' || name || ''' queue_name, 
        decode(state,0,''Ready'',1,''Waiting'',2,''Processed'',3,''Expired'',''?'') state, 
        count(*) count 
        from ' || owner || '.' || queue_table || 
        ' where q_name = ''' || name || ''' group by state' cmd
  FROM all_queues
 WHERE owner IN ('AMADEUS')
 ORDER BY owner, name; 

我能够构建Queue表的所有选择。但我想使用select语句获取输出。

我在考虑管道功能或使用全局临时表。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

DECLARE
   sqlStr VARCHAR2(2000);
BEGIN

   dbms_output.put_line(RPAD('queue_name',30,' ') || RPAD('State',30,' ')  || LPAD('Count',30,' ') );             
   FOR rec_ IN ( SELECT 'select ''' || OWNER || '.' || NAME ||
                        ''' queue_name,          decode(state,0,''Ready'',1,''Waiting'',2,''Processed'',3,''Expired'',''?'') state,          count(*) count          from ' ||
                        OWNER || '.' || QUEUE_TABLE || ' where q_name = ''' || NAME ||
                        ''' group by state' CMD
                 FROM ALL_QUEUES
                 WHERE OWNER IN ('AMADEUS')
                 ORDER BY OWNER, NAME               
               ) LOOP
      sqlStr := 'BEGIN
                    FOR rec_ IN (' || rec_.cmd ||
                                 ') LOOP
                       dbms_output.put_line( RPAD(rec_.queue_name,30,'' '') || RPAD(rec_.State,30,'' '')  || LPAD(rec_.Count,30,'' '') );   
                    END LOOP;
                 END;';
      EXECUTE IMMEDIATE sqlStr ;
   END LOOP;   
END;