2016-05-31 59 views
0

大家好,我有以下功能:調用的Funktion與行類型參數

function Entfernung(v_stadt Rheinlandstädte%rowtype) 
Return Abstandstabelle is 

tabelle ABSTANDSTABELLE; 

cursor c_städte IS 
Select * 
From Rheinlandstädte 
; 
v_andereStadt Rheinlandstädte%rowtype; 
v_entfernung float; 

begin 

Open c_städte; 

    LOOP 

    fetch c_städte into v_andereStadt; 
    v_entfernung := Abstand(v_stadt, v_andereStadt); 
    tabelle(v_andereStadt.stadtname) := v_entfernung; 
    exit when c_städte%NOTFOUND; 
END Loop; 
close c_städte; 
return tabelle; 
end Entfernung; 

我如何可以調用這個函數在oracle中。參數是rowtype,我不能使用DUAL的SELECT函數;

回答

0

一個簡單的例子:

SQL> create table tab_par(a number, b number) 
    2/

Table created. 

SQL> create or replace type tab_numbers as table of number 
    2/

Type created. 

SQL> create or replace function fun_par (r IN tab_par%rowtype) return tab_numbers is 
    2  retVal tab_numbers := new tab_numbers(); 
    3 begin 
    4  retVal.extend(2); 
    5  retVal(1) := r.a; 
    6  retVal(2) := r.b; 
    7  return retVal; 
    8 end; 
    9/

Function created. 

SQL> insert into tab_par values (10, 5); 

1 row created. 

SQL> declare 
    2  r tab_par%rowtype; 
    3  n tab_numbers; 
    4 begin 
    5  select * 
    6  into r 
    7  from tab_par 
    8  where rownum = 1; 
    9  -- call your function 
10  n := fun_par(r); 
11  dbms_output.put_line('n = ' || n(1)); 
12  dbms_output.put_line('n = ' || n(2)); 
13 end; 
14/
n = 10 
n = 5 

PL/SQL procedure successfully completed. 

SQL> 
+0

好的,如果我的函數返回一個表格,而不是數字 – Veni

+0

這沒有什麼不同;只是編輯給你看 – Aleksej

0

這裏是我如何調用這個函數,這個函數是在包:

DECLARE 
v_stadtname_a VARCHAR(20):='Düsseldorf'; 
v_stadtA Rheinlandstädte%rowtype; 
tabelle GEOGRAPHICAL_PACKAGE.ABSTANDSTABELLE; 
v_stadt Rheinlandstädte%rowtype; 
CURSOR c_sta IS 
    SELECT * FROM Rheinlandstädte; 
BEGIN 

SELECT * INTO v_stadtA 
FROM rheinlandstädte 
WHERE stadtname=v_stadtname_a; 


OPEN c_sta; 
LOOP 
    FETCH c_sta INTO v_stadt; 
    EXIT WHEN c_sta%NOTFOUND; 
    tabelle := GEOGRAPHICAL_PACKAGE.Entfernung(v_stadtA); 
    dbms_output.put_line('Abstand zwischen '||v_stadtA.stadtname||' und '|| 
v_stadt.stadtname||' beträgt: '||tabelle(v_stadt.stadtname)||' km.'); 
END LOOP; 
CLOSE c_sta; 

END;

相關問題