2016-04-07 14 views
0

Bu işlevi yaptım, ancak çalıştırdığımda bir hata döndürdü!Satır türünü pl/sql içindeki bir tablodan döndürmek nasıl kullanılır?

create or replace function get_accounts 
(Acc_id in Account1.account_id%Type) 
return account1%rowtype 
as 
l_cust_record account1%rowtype; 
begin 
select * into l_cust_record from account1 
where account_id=Acc_id; 
return(l_cust_record); 
end; 
/
+2

Nasıl çalıştırıyorsunuz? Hata nedir? –

+0

Get_Accounts (account_id) dosyasını yürütün; ve bu hata PLS-00221'dir: 'GET_ACCOUNTS' bir yordam veya tanımlanmamıştır – Sara

+0

SQL Server veya Oracle kullanıyor musunuz. Oracle Pl \ SQL ve SQL Server T-SQL –

cevap

1

Oracle Kurulumu:

CREATE TABLE account1 (
account_id INT, 
name  VARCHAR2(20) 
); 

INSERT INTO account1 VALUES (1, 'Bob'); 

create or replace function get_accounts 
(Acc_id in Account1.account_id%Type) 
return account1%rowtype 
as 
l_cust_record account1%rowtype; 
begin 
select * into l_cust_record from account1 
where account_id=Acc_id; 
return(l_cust_record); 
end; 
/

PL/SQL Blok:

DECLARE 
    r_acct ACCOUNT1%ROWTYPE; 
BEGIN 
    r_acct := get_accounts(1); 
    DBMS_OUTPUT.PUT_LINE(r_acct.name); 
END; 
/

Çıktı:

Bob 
0

Oracle'da bir işlevi çağırmak için, dönüş değerini kullanmanız gerekir. Yani, bir prosedür olarak onu arayamazsın. Böyle bir şey çalışacak:

declare 
    myrow account1%rowtype; 
    account_id Account1.account_id%Type := <VALID ACCOUNT ID VALUE>; 
begin 
    myrow := Get_Accounts(account_id); 
end; 
/
İlgili konular