从from子句的列中获取表名

时间:2017-01-09 04:38:10

标签: sql database oracle plsql procedure

我有一个视图,我有一个表名列,另一列有where子句条件。

    create or replace procedure create_cursor 
    is

    CURSOR v_records is
    select * from t ;

    begin

    FOR temp IN v_records LOOP
        INSERT INTO myTable (id, name)
        select temp.id, temp.name 
        from temp.table where temp.where_clause;

        END LOOP;


    end;
    /

依旧......

现在,我已将记录放在游标中,我想运行每个查询。

dfs.datanode.data.dir

myTable是另一个表,我想将记录放在下一个目的。

2 个答案:

答案 0 :(得分:3)

您需要动态SQL来执行此操作:

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;
/

答案 1 :(得分:3)

@Akshay,

请找到以下代码供您参考。

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;
/