栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

查询表和表中存储的列名

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

查询表和表中存储的列名

使用Peter M的查询来构建SQL文本,然后利用XML的强大功能:

create table attributes (id, entity_id, table_name, column_name)asselect 1, 3, 'VALUES_A', 'VALUE_1' from dual union allselect 2, 2, 'VALUES_B', 'VALUE_3' from dual union allselect 3, 2, 'VALUES_A', 'VALUE_2' from dual;create table values_a (entity_id, value_1, value_2, value_3)asselect 1, 'Monday', 42, 'Green' from dual union allselect 2, 'Sunday', 3000, 'Blue' from dual union allselect 3, 'Wednesday', 1, 'Black' from dual;create table values_b (entity_id, value_1, value_2, value_3)asselect 1, 'Tuesday', 26, 'Green' from dual union allselect 2, 'Saturday', 3, 'Red' from dual union allselect 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_resultfrom   queries;

结果:

TABLE_NAME COLUMN_NAME  ENTITY_ID SQL_RESULT---------- ----------- ---------- ---------------------------------------------------VALUES_A   VALUE_1   3 WednesdayVALUES_B   VALUE_3   2 RedVALUES_A   VALUE_2   2 3000


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/517549.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号