oracle 如何做一个函数来从 pl/sql 中的表中返回行类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36484546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 03:12:35  来源:igfitidea点击:

how to do a function to return row type from a table in pl/sql?

oracleplsql

提问by Sara

I made this function but it return an error when i execute it!

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

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;
/

采纳答案by MT0

Oracle Setup:

甲骨文设置

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 Block:

PL/SQL 块

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

Output:

输出

Bob

回答by GriffeyDog

To call a function in Oracle, you need to use its return value. That is, you can't call it as you would a procedure. Something like this would work:

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

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