Results 1 to 2 of 2

Thread: dynamically binding the tablename

  1. #1
    Join Date
    Mar 2005
    Posts
    22

    dynamically binding the tablename

    How to dynamically bind the table name in stored procedure.

    For example

    procedure sample(tblname varchar2 in)as
    row_count number(2);
    begin
    select count(*) into row_count from tblname;
    end sample;

  2. #2
    Join Date
    Mar 2003
    Posts
    468
    bind variables are used for predicates in the where clause such like :
    create or replace procedure sample(tblname varchar2 ) is
    row_count number;
    begin
    execute immediate 'select count(*) from user_tables where table_name = :1' into row_count using tblname;
    dbms_output.put_line('result : ' ||row_count);
    end sample;

    if you want to dynamically provide a table name you need to build a sql string and execute it like this:

    create or replace procedure sample(tblname varchar2 ) is
    row_count number;
    begin
    execute immediate 'select count(*) from '||tblname into row_count;
    dbms_output.put_line('result : ' ||row_count);
    end sample;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •