2017-05-04 10 views
0

Hallo i erstellen möchten eine Funktion, die mehrere Werte in nur eine Zeile zurück:Oracle PLSQL Rückkehr einer Reihe Typ

create or replace TYPE foo_type AS OBJECT (
col1 NUMBER    , 
col2 VARCHAR2(150 BYTE) 
); 

CREATE FUNCTION foo_function() RETURN foo_type 
as 
row_type foo_type; 

select 
b1, -- NUMBER TYPE    
b2 -- VARCHAR2(150 BYTE) TYPE 
into 
row_type 
from 
table_xxx 
where 
rownum=1; --Only one row! 

return row_type; 

END foo_function; 

Wenn ich kompilieren i erhalten: ORA-00947 nicht genug Werte

ich habe versucht:

select 
b1, -- NUMBER TYPE    
b2 -- VARCHAR2(150 BYTE) TYPE 
into 
row_type.col1, 
row_type.col2 
from 
table_xxx 
where 
rownum=1; --Only one row! 

und die Funktion kompiliert aber wenn id Lauf:

select foo_function() from dual; 

Oracle Rückkehr: ORA-06530 Bezug auf nicht initialisierten Verbund

Antwort

1

Sie es als Objekt auf Datenbankebene definiert, so hat dieses Objekt zu initialisiert werden.

Sie könnten entweder das tun sie mit null Werte zu initialisieren, bevor Sie wählen, wie Sie folgendermaßen vorgehen:

row_type := foo_type(null,null) 

Aber die passendere Lösung wäre hier die Auswahl, die der folgenden Wechsel:

select 
foo_type(b1,b2) -- Create a foo_type from the select 
into 
row_type -- throw this initialized foo_type into your variable row_type 
from 
table_xxx 
where 
rownum=1; --Only one row! 
+1

Antwort ist in Ordnung, aber ich kann mit Ihnen nicht einverstanden über 'Sie es als Objekt auf Datenbankebene definiert, so hat dieses Objekt initialisiert werden.Selbst eine auf Unterprogramm-Ebene definierte Typvariable muss initialisiert werden. – Seyran

1

wirft ORA-00947 not enough values, da gibt es zwei Werte b1 und b2 aber nur eine Variabel le row_type, um sie einzufügen.

Sie können entweder tun:

CREATE FUNCTION foo_function() RETURN foo_type 
as 
    row_type foo_type; 
BEGIN      -- missing BEGIN 
    select foo_type (b1, b2) -- You need to put the values into a foo_type object 
    into row_type 
    from table_xxx 
    where rownum=1; 

    return row_type; 
END foo_function; 

Oder:

CREATE FUNCTION foo_function() RETURN foo_type 
as 
    row_type foo_type := foo_type(NULL, NULL); -- Initialise the object. 
BEGIN 
    select b1,   b2 
    into row_type.col1, row_type.col2 
    from table_xxx 
    where rownum=1; 

    return row_type; 
END foo_function; 
+0

Danke jetzt, wenn ich folgendes mache: wähle foo_function() aus dual; Ich reviewed: [foo_type] Wie kann ich ALLE Wert einfügen in zurückgegebenen Datensatz? –

+0

@MicheleM 'SELECT foo.col1, foo.col2 VON (SELECT foo_function() AS foo FROM DUAL)' – MT0

+0

Und zu tun: SELECT col3, col4, (SELECT foo.col1, foo.col2 VON (SELECT foo_function() AS foo FROM DUAL)) von tab_yyy - tab_yyy hat Spalte col3 und col4 –

Verwandte Themen