如何从pl / sql中的表中返回行类型的函数?

时间:2016-04-07 18:38:02

标签: oracle plsql

我创建了这个函数,但是当我执行它时它返回一个错误!

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 :(得分:2)

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

答案 1 :(得分:0)

要在Oracle中调用函数,您需要使用其返回值。也就是说,你不能像过程一样调用它。像这样的东西会起作用:

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