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

2021-12-24 00:00:00 oracle plsql

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

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

推荐答案

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

相关文章