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

后端 未结 2 1953
终归单人心
终归单人心 2020-12-11 07:18

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%r         


        
相关标签:
2条回答
  • 2020-12-11 07:47

    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:

    declare
      myrow account1%rowtype;
      account_id Account1.account_id%Type := <VALID ACCOUNT ID VALUE>;
    begin
      myrow := Get_Accounts(account_id); 
    end;
    /
    
    0 讨论(0)
  • 2020-12-11 07:48

    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:

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

    Output:

    Bob
    
    0 讨论(0)
提交回复
热议问题