passing in table name as plsql parameter

前端 未结 1 448
轻奢々
轻奢々 2020-12-02 02:14

I want to write a function to return the row count of a table whose name is passed in as a variable. Here\'s my code:

create or replace function get_table_co         


        
相关标签:
1条回答
  • 2020-12-02 02:54

    You can use dynamic SQL:

    create or replace function get_table_count (table_name IN varchar2)
      return number
    is
      table_count number;
    begin
      execute immediate 'select count(*) from ' || table_name into table_count;
      dbms_output.put_line(table_count);
      return table_count;
    end;
    

    There is also an indirect way to get number of rows (using system views):

    create or replace function get_table_count (table_name IN varchar2)
      return number
    is
      table_count number;
    begin
      select num_rows
        into table_count
        from user_tables
       where table_name = table_name;
    
      return table_count;
    end;
    

    The second way works only if you had gathered statistics on table before invoking this function.

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