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

后端 未结 2 1962
终归单人心
终归单人心 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: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
    

提交回复
热议问题