fetch table name from a column for from clause

前端 未结 2 1835
无人及你
无人及你 2020-12-20 10:57

I have a view t with me which has a column for table name and another column which has where clause condition.

    id| name|table_in| where_clause

    1 | S         


        
相关标签:
2条回答
  • 2020-12-20 11:03

    @Akshay,

    Please find the code below for your reference.

    Create or replace procedure create_cursor is
    l_statement varchar2(32767);
    cursor v_records is
      select * from t;
    begin
    for temp in v_records
    loop
      l_statement := 'INSERT INTO myTable (id, name) select '||temp.id||','
       ||temp.name|| ' from ' || temp.table1 
       || ' where ' || temp.where_clause;
    
      execute immediate l_statement;
      end loop;
    end;
    /
    
    0 讨论(0)
  • 2020-12-20 11:23

    You need dynamic sql to do this:

    CREATE OR REPLACE PROCEDURE create_cursor
    IS
      l_statement VARCHAR2(32767);
      CURSOR v_records
      IS
        SELECT * FROM t;
    BEGIN
      FOR temp IN v_records
      LOOP
        l_statement := 'INSERT INTO myTable (id, name)
              select id, name from ' || temp.table || 
              ' where ' || temp.where_clause;
        EXECUTE immediate l_statement;
      END LOOP;
    END;
    /
    
    0 讨论(0)
提交回复
热议问题