create table w (a number);declare
  str varchar2(1000); 
    CURSOR tname_cursor IS 
   SELECT table_name 
    FROM user_tables 
    where table_name like 'W'; 
    
       
    begin 
    
  FOR x IN tname_cursor LOOP 
  str := 'drop table '||x.table_name; 
  dbms_output.put_line(str);
  EXECUTE IMMEDIATE str; 
  
  END LOOP; 
  END; 
  /
里这句  FOR x IN tname_cursor LOOP 
  str := 'drop table '||x.table_name; 中不是变量啊,怎么也可以运行呢?而且执行结果还是正常的

解决方案 »

  1.   

    declare
      str varchar2(1000);  
      CURSOR tname_cursor IS  
      SELECT table_name  
      FROM user_tables  
      where table_name like 'W%';  
        
        
      begin  
        
      FOR x IN tname_cursor LOOP  
      str := 'drop table '||x.table_name;  
      dbms_output.put_line(str);
      EXECUTE IMMEDIATE str;  
       
      END LOOP;  
      END;  
      /
      

  2.   

    scott@LYMORA>  create table w (a number);表已创建。已用时间:  00: 00: 00.04
    scott@LYMORA> desc w;
     名称                                                              是否为空? 类型
     ----------------------------------------------------------------- -------- -----------------------------------------
     A                                                                          NUMBERscott@LYMORA> declare
      2    str varchar2(1000);
      3    CURSOR tname_cursor IS
      4    SELECT table_name
      5    FROM user_tables
      6    where table_name like 'W%';
      7
      8
      9    begin
     10
     11    FOR x IN tname_cursor LOOP
     12    str := 'drop table '||x.table_name;
     13    dbms_output.put_line(str);
     14    EXECUTE IMMEDIATE str;
     15
     16    END LOOP;
     17    END;
     18    /
    drop table WPL/SQL 过程已成功完成。已用时间:  00: 00: 00.11
    scott@LYMORA> desc w;
    ERROR:
    ORA-04043: 对象 w 不存在
    scott@LYMORA>
      

  3.   

    x 默认就是 tname_cursor 的记录类型。
    在循环中,其实执行了
    open tname_cursor;
    fetch tname_cursor into x;
    的操作。
      

  4.   


    -- x 这只是一个变量的名字! 你要这样问的话,那你的游标名为什么也可以随便写呢?-- 在你出生,还没有命名之时,你的名字是不是可以随便取,但是一旦注册了(有身份证了),是不是修改名……
    [/Quote]
    -- 是的,但变量名不能以数据开头!
      

  5.   


    -- 建议去看一看 oracle 变量命名规则!
      

  6.   

    谢了,我已经结贴了,我思维固化了。在想着,定义游标,打开游标,获取数据,关闭游标,所以没想到这个X是已经做了两步动作了,就是hidanger0(网络隐患)
    说的
    x 默认就是 tname_cursor 的记录类型。
    在循环中,其实执行了
    open tname_cursor;
    fetch tname_cursor into x;
    的操作。
    呵呵,在次感谢两位