2016-04-07 3 views
0

私はこの機能を作ったが、私はそれを実行するとエラーを返す!pl/sqlのテーブルから行の型を返す関数を実行する方法は?

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

どのように実行していますか?エラーは何ですか? –

+0

Get_Accounts(account_id)を実行します。 これはエラーPLS-00221です。 'GET_ACCOUNTS'はプロシージャではないか、または未定義です – Sara

+0

SQL ServerまたはOracleを使用していますか。 OracleはPl \ SQLでSQL ServerはT-SQL –

答えて

1

Oracleのセットアップ

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ブロック

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

出力

Bob 
0

Oracleで関数を呼び出すには、戻り値を使用する必要があります。つまり、プロシージャと同じように呼び出すことはできません。このような何かが働くだろう:

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