declared num number;
 begin       
    select       count(1)       into       num       from       ...;       
    if       num> 0       then       
    execute       immediate       ....;       
    end       if;       
    execute       immediate      ....;       
    end;  
报错“名称已由现有对象是用”
删去 declare 又要求说明num。

解决方案 »

  1.   

    declared num number;
    这个是笔误吧如果按照你的语句,应该是没问题
    你把完整的贴出来吧
      

  2.   

    你是否在execute后的语句里create table?
      

  3.   

    declare
     num number; 
    begin      
        select      count(1)      into      num      from      ...;      
        if  num> 0  then      
            execute      immediate      ....;      
        end  if;      
        execute      immediate      ....;      
    end;  
      

  4.   

    我猜楼主是否在execute后面使用了创建表之类的语句
    而且两个execute里的表名一致
    当num>0时,执行两次
    造成出错
      

  5.   

    declare num  number;       
    begin       
    select  count(*) into  num  from  user_tables  where  table_name= 'NATION ';       
    if num> 0  then       
    execute  immediate  'drop table '||'NATION';       
    end   if; 
    execute  immediate  'CREATE TABLE nation (
    nationcode VARCHAR2(50) NOT NULL,
    nationname VARCHAR2(40),
    PRIMARY KEY(nationcode)
    ) ';       
    end; 
    结果:
    declare num  number;
    *
    ERROR 位于第 1 行:
    ORA-00955: 名称已由现有对象使用
    ORA-06512: 在line 7
    注释掉declare num  number;
    结果:
    select  count(*) into  num  from  user_tables  where  table_name= 'NATION ';
                           *
    ERROR 位于第 2 行:
    ORA-06550: 第 2 行, 第 24 列:
    PLS-00201: 必须说明标识符 'NUM'
    ORA-06550: 第 2 行, 第 29 列:
    PL/SQL: ORA-00904: : 无效的标识符
    ORA-06550: 第 2 行, 第 1 列:
    PL/SQL: SQL Statement ignored
    ORA-06550: 第 3 行, 第 4 列:
    PLS-00201: 必须说明标识符 'NUM'
    ORA-06550: 第 3 行, 第 1 列:
    PL/SQL: Statement ignored
      

  6.   

    你的错误是在 where table_name = 'NATION '
    你表名多了一个空格
      

  7.   

    declare num  number;
    begin
    select  count(*) into  num  from  user_tables  where  table_name= 'NATION';
    if num> 0  then
    execute  immediate  'drop table '||'NATION';
    end  if;
    execute  immediate  'CREATE TABLE nation (
    nationcode VARCHAR2(50) NOT NULL,
    nationname VARCHAR2(40),
    PRIMARY KEY(nationcode)
    ) ';
    end;
    /-------------------------
    7楼正解,where的时候表名多了个空格
      

  8.   

    declare num  number;      
    begin      
    select  count(*) into  num  from  user_tables  where  table_name= 'NATION';  --表名后面有空格,查询出来的num恒等于0,所以报错    
    if num> 0  then      
    execute  immediate  'drop table '||'NATION';      
    end  if; 
    execute  immediate  'CREATE TABLE nation ( 
    nationcode VARCHAR2(50) NOT NULL, 
    nationname VARCHAR2(40), 
    PRIMARY KEY(nationcode) 
    ) ';      
    end;