2011-07-26 82 views

回答

3

這是一個鈍器的方法來解決這個問題。

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; 
/

因此,考慮到本次測試數據...

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> 

...我們得到如下的輸出:

SQL> select * from table (col_diff(8101,8102)) 
    2/

COL_NAME 
------------------------------ 
OLD_VAL 
------------------------------------------------------------------- 
NEW_VAL 
------------------------------------------------------------------- 
HIREDATE 
03-DEC-2010 
02-JAN-2011 


SQL> 

現在,毫無疑問,你會喜歡的東西是不詳細。我認爲可以使用11g中引入的增強的方法4動態SQL來完成某些工作。唉,你說你正在使用10g。