Hier is een botte instrumentbenadering van het probleem.
create or replace function col_diff
( p_empno_1 in emp.empno%type
, p_empno_2 in emp.empno%type )
return col_nt pipelined
is
out_val col_t := new col_t(null, null, null);
emp_rec1 emp%rowtype;
emp_rec2 emp%rowtype;
begin
select *
into emp_rec1
from emp
where empno = p_empno_1;
select *
into emp_rec2
from emp
where empno = p_empno_2;
if emp_rec1.ename != emp_rec2.ename
then
out_val.col_name := 'ENAME';
out_val.old_val := emp_rec1.ename;
out_val.new_val := emp_rec2.ename;
pipe row (out_val);
end if;
if emp_rec1.hiredate != emp_rec2.hiredate
then
out_val.col_name := 'HIREDATE';
out_val.old_val := to_char(emp_rec1.hiredate, 'DD-MON-YYYY');
out_val.new_val := to_char(emp_rec2.hiredate, 'DD-MON-YYYY');
pipe row (out_val);
end if;
return;
end;
/
Dus, gezien deze testgegevens...
SQL> select empno, ename, hiredate
2 from emp
3 where empno > 8100
4 /
EMPNO ENAME HIREDATE
---------- ---------- ---------
8101 PSMITH 03-DEC-10
8102 PSMITH 02-JAN-11
SQL>
... we krijgen deze uitvoer:
SQL> select * from table (col_diff(8101,8102))
2 /
COL_NAME
------------------------------
OLD_VAL
-------------------------------------------------------------------
NEW_VAL
-------------------------------------------------------------------
HIREDATE
03-DEC-2010
02-JAN-2011
SQL>
Nu zou je ongetwijfeld iets minder uitgebreid willen hebben. Ik denk dat het mogelijk is om iets te doen met de verbeterde dynamische SQL van Method 4 die in 11g is geïntroduceerd. Helaas, je zegt dat je 10g gebruikt.