2016-04-07 58 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

甲骨文設置

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; 
/
相關問題