declare a number;
begin
select count(*) into a from user_tables where table_name='TABLE1';
if a > 0 then
   return;
else  
begin
create table TABLE1
(
  ID         INTEGER not null,
)
end
  end if
  end
我想检查一张表是否存在,不存在时创建它,但以上语句有问题,请指点下

解决方案 »

  1.   

    另外,还有一点,在程序块里,是不能直接执行 DDL 语句的
    你可以是用 execute immediate 执行动态语句的方式执行 DDL 语句
      

  2.   


    你 end, end if 不是都没加“;”结束符吗?
    还有,把 create 语句写成字符串,execute immediate sql_string;
      

  3.   

    declare a number;
    begin
    select count(*) into a from user_tables where table_name='TABLE1';
    if a > 0 then
       return;
    else  
    create table TABLE1
    (
      ID         INTEGER not null,
    );
      end if;
      end
      

  4.   


    还是给你写一个示例好了declare
        stabname     varchar2(30);
        icount       number;
        sql_string   varchar2(200);
    begin
        stabname := 'TABLE1';
        select count(1) into icount from tabs where table_name = stabname;
        if (icount = 0) then
            sql_string := 'create table ' || stabname || ' (id number)';
            execute immediate sql_string;
        end if;
    end;
    /
      

  5.   

    declare a number;
    declare b varchar2(200);
    b:='create table TABLE1 (ID INTEGER not null)';
    begin
    select count(*) into a from user_tables where table_name='TABLE1';
    if a > 0 then
      return;
    else  execute immediate b;  end if;
      end
    这样写了下还是有问题呢,能不能给写个例子看看
      

  6.   


    oracle declare 是声明开始,并不是定义一个变量就写一个 declare
      

  7.   


    哦,这样,但我去掉后面的declare后为什么还是有错呢?我好像有点罗嗦哈