【问题标题】:Dynamically selecting table name using sub-query in from clause使用 from 子句中的子查询动态选择表名
【发布时间】:2014-02-11 11:49:33
【问题描述】:

我有一个以源和表名作为属性的映射表。

我有以下查询:

select *
from 
(select table_name from mapping_table where source='CL Table')

我想检索table_name 的数据,但是这个查询将只返回表名而不是数据。

怎么做?

【问题讨论】:

  • 你必须让它动态化。这么多方法是可能的!但问题是使用其中的列名。因为您可能不知道其中的列。如果您事先知道列名,那就很简单了!
  • 你用什么客户端?例如:JAVA(JDBC)/PHP(ADO) ?
  • 也许这个问题可以帮助stackoverflow.com/questions/21555997/…

标签: oracle dynamic


【解决方案1】:
 DECLARE
    v_mystring VARCHAR(50);
    v_my_ref_cursor sys_refcursor;
    myrecord <some_table_or_type>;
  BEGIN

    select table_name into my_table_name
      from mapping_table 
    where source='CL Table';

    v_mystring := 'SELECT * from '||my_table_name;;

    OPEN v_my_ref_cursor FOR v_mystring;

    LOOP
      FETCH v_my_ref_cursor INTO myrecord;
      -- your processing
    END LOOP;
    CLOSE v_my_ref_cursor;
  END;

【讨论】:

  • 那么,现在我必须在 from 子句中调用一个 proc 吗?我可以将整个查询结果集从 proc 返回到 from 子句吗?有可能吗?
猜你喜欢
  • 2012-07-04
  • 1970-01-01
  • 2019-04-22
  • 1970-01-01
  • 2014-02-12
  • 2012-10-28
  • 2019-08-27
  • 2021-08-18
  • 1970-01-01
相关资源
最近更新 更多