declare
numb number;
begin
select count(*) into numb from user_tables where table_name='HAOMA';
if numb>0 then
execute immediate 'drop table haoma';
end if;
execute immediate 'create table haoma as select distinct service_id from service';
end;请问这个哪里有问题呀?

解决方案 »

  1.   

     -- 语法没问题,逻辑不对
    declare
      numb number;
    begin
      select count(*) into numb from user_tables where table_name = 'HAOMA';
      if numb > 0 then
        execute immediate 'drop table haoma';
        execute immediate 'create table haoma as select distinct service_id from service';
     end if;
    end;
      

  2.   


    --这样?
    declare 
    numb number; 
    begin 
    select count(*) into numb from user_tables where table_name='HAOMA'; 
    if numb>0 then 
       execute immediate 'drop table haoma'; 
       execute immediate 'create table haoma as select distinct service_id from service'; 
    end if; 
    end; 
      

  3.   

    if numb=0时,不会drop表,但create表的语句还是会执行,这样就会出错,所以drop、create表要都放在if语句里面
      

  4.   

    numb=0说明没有这个表啊,所以执行建表没什么问题吧
      

  5.   

    declare numb number; begin select count(*) into numb from user_tables where table_name='HAOMA'; if numb>0 then execute immediate 'drop table haoma'; execute immediate 'create table haoma as select distinct service_id from service'; end if; end;