简体   繁体   中英

to call a funktion with a rowtype Parameter

hello everyone i have the following function:

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;

how can i call this function in oracle. The parameter is rowtype and I cant't use SELECT function from DUAL;

A simple example:

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>

here is how I call this function, this function is in the package:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM