declare
cursor cc is select distinct substr(vc_mobileno,1,3) trd from dw_foxuser_det order by 1;
rec_ind number;
ccrec cc%rowtype;
begin
 select count(*) into rec_ind from user_table where table_name = upper('tp_20120807_a01');
 if (rec_ind <> 0) then
Execute immediate 'drop table tp_20120807_a01 purge';
Execute immediate 'create table tp_20120807_a01 (test varchar2(11))';
end if;
 for ccrec in cc loop
-- 下面这一句,如果使用动态语句...
 insert into tp_20120807_a01 select vc_mobileno from dw_foxuser_det where substr(vc_mobileno,4,3) = ccrec.trd;
commit;
end loop;
End;
-- 如示例所示,如果我使用上面的代码执行,在表tp_20120807_a01 存在的情况自然没问题,但是如果不存在,在for 循环中的insert 语句在执行时是通不过的,因为表不存在,这时我想使用动态语句执行:Execute immeidiate 'insert into tp_20120807_a01 select vc_mobileno from dw_foxuser_det where substr(vc_mobileno,4,3) = ccrec.trd';
这时也会报错,提示是ccrec.trd 未定义,如何回避这个问题的产生?

解决方案 »

  1.   


    --你的ccrec.trd是外面的值
    Execute immeidiate 'insert into tp_20120807_a01 select vc_mobileno from dw_foxuser_det where substr(vc_mobileno,4,3) = '||ccrec.trd;
      

  2.   

    嘿嘿,厉害!第一次见动态语句还可以这样拉开写!
    但是这样虽然ccrec.trd 不会提示未定义,但提示表不存在了..。虽然有单引号括起来,但好像动态部分没被认出来。 
      

  3.   

    已经解决, zhangandli的方式是对的,我敲错了。感谢。
      

  4.   

    补充说明一下情况——
    报表的原因是在于那句检查语句,应该把建表的语句写在end if 的外面。这样问题就解决了,这是一个逻辑错误,SORRY...