Have unpivot automatically grab column list (oracle 11g)

。_饼干妹妹 提交于 2020-01-15 08:02:10

问题


This is a follow up question to Transpose one row into many rows Oracle

I want to be able to unpivot an arbitrary query result.

To unpivot a table manually, I would do:

select value_type, value from (
 (
  -- query to be unpivoted
  -- EG: select col1, col2, col3, col4, col5 from table
 )
 unpivot
 (
  -- Line I would like to change
  value for value_type in (col1, col2, col3, col4, col5)
 )
);

This works for all queries that return 5 columns, called col1, col2, etc. Is there something I put in instead of value for value_type in (col1, col2, col3, col4, col5) that will grab all the column names from the query/view/table that is selected in the first part?


回答1:


You could create a stored procedure to do this in PL/SQL by dynamically creating your SQL statement as a string an then using execute immediate to execute it and return a cursor.



来源:https://stackoverflow.com/questions/2345010/have-unpivot-automatically-grab-column-list-oracle-11g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!