查询存储在表中的表和列名

时间:2018-04-24 11:52:24

标签: sql oracle dynamic

我来到这里是因为我在Oracle数据库中遇到的问题经验不足以解决。让我解释一下:

表格

我有一个表格,我们称之为属性,包含3列: ID ,属性的ID, -EDIT:Entity_ID以及它引用的实体/ EDIT - Table_name ,包含存储属性值的表的名称, Column_name ,包含列的名称在该表中存储了值。

Table_name 列中引用的每个表都包含相同的列名称(例如 Value1 Value2 等等。)除了第一个引用属性链接到的另一个实体( Entity_id )之外。

我在找什么

我的目标是构建一个选择每个属性(基于其id)及其值的查询。但我不知道的是如何查询,因为表的名称和列的更改。 有没有办法使用变量?但如果是这样,我怎样才能将它们放入查询中,以便每行自动更改?

修改

实施例

ATTRIBUTES table
ID         ENTITY_ID      TABLE_NAME   COLUMN_NAME
---------- -------------- ------------ -----------
1          3              Values_A     Value_1  
2          2              Values_B     Value_3
3          2              Values_A     Value_2

VALUES_A table
ENTITY_ID  Value_1        Value_2      Value_3
---------- -------------- ------------ -----------
1          Monday         42           Green
2          Sunday         3000         Blue
3          Wednesday      1            Black

VALUES_B table
ENTITY_ID  Value_1        Value_2      Value_3
---------- -------------- ------------ ------------
1          Tuesday        26           Green
2          Saturday       3            Red
3          Wednesday      15           White

所以我要找的结果是:

RESULT:
ID        Value
--------- -----------
1         Wednesday
2         Red
3         3000  

对不起,如果看这很痛苦,那就太痛苦了(没找到如何更好地格式化)

2 个答案:

答案 0 :(得分:2)

这很难看

但它会生成一个你可以运行的sql语句,也许它不是你想要的

select 'select '|| column_name || ' from ' || table_name || ' where entity_id = ' || entity_id || case when id = max_id then '' else ' union all ' end
from 
  (select a.*, max(a.id) over (order by id) max_id 
   from attributes a)

答案 1 :(得分:2)

使用Peter M的查询构建SQL文本,然后利用XML的黑暗力量:

create table attributes (id, entity_id, table_name, column_name)
as
select 1, 3, 'VALUES_A', 'VALUE_1' from dual union all
select 2, 2, 'VALUES_B', 'VALUE_3' from dual union all
select 3, 2, 'VALUES_A', 'VALUE_2' from dual;

create table values_a (entity_id, value_1, value_2, value_3)
as
select 1, 'Monday', 42, 'Green' from dual union all
select 2, 'Sunday', 3000, 'Blue' from dual union all
select 3, 'Wednesday', 1, 'Black' from dual;

create table values_b (entity_id, value_1, value_2, value_3)
as
select 1, 'Tuesday', 26, 'Green' from dual union all
select 2, 'Saturday', 3, 'Red' from dual union all
select 3, 'Wednesday', 15, 'White' from dual;

查询:

with queries as
     ( select table_name, column_name, entity_id
            , 'select '|| column_name || ' as c from ' || table_name ||
             ' where entity_id = ' || entity_id ||
              case
                  when id = max_id then ''
                  else ' union all '
              end as sqltext
       from 
           ( select a.*, max(a.id) over (order by id) max_id from attributes a ) )
select table_name, column_name, entity_id
     , extractvalue(xmltype(dbms_xmlgen.getxml(sqltext)),'/ROWSET/ROW/C') as sql_result
from   queries;

结果:

TABLE_NAME COLUMN_NAME  ENTITY_ID SQL_RESULT
---------- ----------- ---------- ---------------------------------------------------
VALUES_A   VALUE_1              3 Wednesday
VALUES_B   VALUE_3              2 Red
VALUES_A   VALUE_2              2 3000