有没有办法在Oracle DB中水平打印查询结果

时间:2015-09-17 09:13:39

标签: sql oracle

Select * from TABLENAME WHERE "CLAUSE"

它会将结果打印在一行中。

Col 1    Col 2 ......  Col N                                                 
Val 1    Val 2 ......  Val N

我需要

Col 1  Val 1            
Col 2  Val 2        
.       
.          
.        
Col N  Val N

2 个答案:

答案 0 :(得分:1)

定期做一点时间,但是:

select COL_NAME, COL_DATA
from  (SELECT * FROM table_name 
       WHERE clause)
  unpivot ( COL_NAME FOR COL_DATA IN ( COL1     as 'COL1'
                                      ,COL2     as 'COL2'
                                      ,COL3     as 'COL3'
                                      ,COL4     AS 'COL4') 
           )

请记住,您还需要将所有值转换为相同的数据类型,因为Oracle不会在同一列中混合数据类型,因此如果COL1-3是数字,但COL4是varchar,那么您将

select COL_NAME, COL_DATA
from  (SELECT * FROM table_name 
       WHERE clause)
  unpivot ( COL_NAME FOR COL_DATA IN ( TO_CHAR(COL1) as 'COL1'
                                      ,TO_CHAR(COL2) as 'COL2'
                                      ,TO_CHAR(COL3) as 'COL3'
                                      ,COL4          AS 'COL4') 
           )

答案 1 :(得分:0)

select 'col 1', col1 from TABLENAME WHERE "CLAUSE"
UNION ALL
select 'col 2', col2 from TABLENAME WHERE "CLAUSE"
UNION ALL
...
select 'col n', coln from TABLENAME WHERE "CLAUSE"
order by 1