如何在执行立即时将表名添加到动态查询中?

时间:2015-10-30 13:49:26

标签: sql oracle syntax plsql execute-immediate

我很困惑,因为我不知道如何在execute immediate子句中使用变量作为字符串。

declare
  variable1 varchar2(30):
  cur sys_refcursor;
begin
  open cur for
    select tablename from table1;

  loop
    fetch cur into variable1;
    exit when cur %notfound;

    execute immediate 'select count(*)
                         into variable1
                         from user_tables
                        where table_name =''' || variable1 || '''';
  end loop;

  close cur;

end;

此处variable1是表名。应该有一个字符串值。怎么做?仍然有错误。

1 个答案:

答案 0 :(得分:1)

execute immediate语句应如下所示:

execute immediate 'select count(*) from user_tables where table_name ='''||variable1||'''' into variable1;

在查询后进入xxx。