使用oracle数据库,pl\sql developer开发环境,system用户登入输入以下代码后,会提示
“ora-02289:序列号不存在,ora-06512:在line5"
为什么会出现这种情况,怎么解决,请高手指教!
declare
v_sql   varchar2(200); 
begin
v_sql:='drop sequence cxexam_tmp_id';
execute immediate v_sql;
v_sql:='create sequence cxexam_tmp_id increment by 1 start with 1 maxvalue 9999999 nocycle nocache';
execute immediate v_sql; 
end;

解决方案 »

  1.   

    运行这些语句之前,sequence cxexam_tmp_id 不存在。
    所以不能删除。
      

  2.   

    select count(*) from user_indexes where index_name='cxexam_tmp_id'; 然后根据count(*)的值判断是否存在再用drop
      

  3.   

    不要这样做,你是不是可以写成这样declare 
    v_sql  varchar2(200); 
    begin 
    v_sql:='create or replace sequence cxexam_tmp_id increment by 1 start with 1 maxvalue 9999999 nocycle nocache';
    execute immediate v_sql; 
    end;
      

  4.   

    declare 
    v_sql  varchar2(200); 
    begin 
    v_sql:='drop sequence cxexam_tmp_id'; 
    execute immediate v_sql; 
    exception when others then null;  ---加上这句即可
    v_sql:='create sequence cxexam_tmp_id increment by 1 start with 1 maxvalue 9999999 nocycle nocache'; 
    execute immediate v_sql; 
    end;
      

  5.   

    方法1:
    declare
      type cursor_t is ref cursor;
      cursor_s cursor_t;
      v_seqname varchar2(36);
    begin
      v_seqname := '';
      open cursor_s for 'select sequence_name from user_sequences where sequence_name=''CXEXAM_TMP_ID''';
      loop
        fetch cursor_s into v_seqname;
        exit when cursor_s%notfound;
      end loop;
      close cursor_s;
      if v_seqname='' then
         execute immediate 'create sequence cxexam_tmp_id increment by 1 start with 1 maxvalue 9999999 nocycle nocache';
      else
         dbms_output.put_line('此序列已经存在');
      end if;
    end;
    /方法2:
    declare
      v_i integer;
    begin
      select count(*) into v_i from user_sequences where sequence_name='SEQ_ID1';
      if v_i>0 then
        execute immediate 'create sequence SEQ_ID1 increment by 1 start with 1 maxvalue 9999999';
      end if;
    end;
    /
      

  6.   

    谢谢各位,问题已解决,开始先新建一个序列cxexam_tmp_id就可以。
      

  7.   

    create or replace 不行吗?为什么要先判断?