Friday, December 21, 2007

Simulating print_table with unpivot

With the advent of UNPIVOT in Oracle11g, a SQL only solution for printing table contents vertically has been made a little easier. I've used Tom Kyte's print_table function for this purpose occasionally. You can read about this function here on AskTom. The following SQL using unpivot is definitely not easier than issuing

exec print_table('select * from emp')
, so it's just for fun:

rwijk@ORA11G>  select decode
2 ( col
3 , 'LINE', '======================'
4 , rpad(col,8) || ': ' || value
5 ) "Contents of table EMP"
6 from ( select to_char(empno) empno
7 , ename
8 , job
9 , to_char(mgr) mgr
10 , to_char(hiredate,'dd-mm-yyyy') hiredate
11 , to_char(sal) sal
12 , to_char(comm) comm
13 , to_char(deptno) deptno
14 , '=' line
15 from emp
16 )
17 unpivot include nulls
18 (value for col in (empno,ename,job,mgr,hiredate,sal,comm,deptno,line))
19 /

Contents of table EMP
--------------------------------------------------
EMPNO : 7369
ENAME : SMITH
JOB : CLERK
MGR : 7902
HIREDATE: 17-12-1980
SAL : 800
COMM :
DEPTNO : 20
======================
EMPNO : 7499
ENAME : ALLEN
JOB : SALESMAN
MGR : 7698
HIREDATE: 20-02-1981
SAL : 1600
COMM : 300
DEPTNO : 30
======================
EMPNO : 7521
ENAME : WARD
JOB : SALESMAN
MGR : 7698
HIREDATE: 22-02-1981
SAL : 1250
COMM : 500
DEPTNO : 30
======================
EMPNO : 7566
ENAME : JONES
JOB : MANAGER
MGR : 7839
HIREDATE: 02-04-1981
SAL : 2975
COMM :
DEPTNO : 20
======================
EMPNO : 7654
ENAME : MARTIN
JOB : SALESMAN
MGR : 7698
HIREDATE: 28-09-1981
SAL : 1250
COMM : 1400
DEPTNO : 30
======================
EMPNO : 7698
ENAME : BLAKE
JOB : MANAGER
MGR : 7839
HIREDATE: 01-05-1981
SAL : 2850
COMM :
DEPTNO : 30
======================
EMPNO : 7782
ENAME : CLARK
JOB : MANAGER
MGR : 7839
HIREDATE: 09-06-1981
SAL : 2450
COMM :
DEPTNO : 10
======================
EMPNO : 7788
ENAME : SCOTT
JOB : ANALYST
MGR : 7566
HIREDATE: 09-12-1982
SAL : 3000
COMM :
DEPTNO : 20
======================
EMPNO : 7839
ENAME : KING
JOB : PRESIDENT
MGR :
HIREDATE: 17-11-1981
SAL : 5000
COMM :
DEPTNO : 10
======================
EMPNO : 7844
ENAME : TURNER
JOB : SALESMAN
MGR : 7698
HIREDATE: 08-09-1981
SAL : 1500
COMM : 0
DEPTNO : 30
======================
EMPNO : 7876
ENAME : ADAMS
JOB : CLERK
MGR : 7788
HIREDATE: 12-01-1983
SAL : 1100
COMM :
DEPTNO : 20
======================
EMPNO : 7900
ENAME : JAMES
JOB : CLERK
MGR : 7698
HIREDATE: 03-12-1981
SAL : 950
COMM :
DEPTNO : 30
======================
EMPNO : 7902
ENAME : FORD
JOB : ANALYST
MGR : 7566
HIREDATE: 03-12-1981
SAL : 3000
COMM :
DEPTNO : 20
======================
EMPNO : 7934
ENAME : MILLER
JOB : CLERK
MGR : 7782
HIREDATE: 23-01-1982
SAL : 1300
COMM :
DEPTNO : 10
======================

126 rijen zijn geselecteerd.


Note that I have to convert all columns to varchar2's to be able to unpivot them. Also, the non default "including nulls" is used to explicitly generate rows when the COMM column is null.

No comments:

Post a Comment

Post a Comment