Oracle SQL“EXECUTE IMMEDIATE”ORA-00922/06512创建表

时间:2017-09-19 07:22:44

标签: sql oracle execute-immediate

我搜索了很多,很多人遇到了类似的问题,但似乎没有什么匹配。

这没问题:

create table CUSTOM_WORKFLOW_ID_COUNTER (
    id_ NUMBER (19) GENERATED by default on null as IDENTITY,
    type_ VARCHAR2(50 CHAR),
    masterId_ VARCHAR2(100 CHAR),
    counter_ NUMBER (19),
    primary key (id_)
    );

由于Oracle缺乏对“ drop table if exists ”的支持,我必须检查该表是否以其他方式存在:

  DECLARE cnt NUMBER;
  BEGIN
   SELECT COUNT(*) INTO cnt FROM user_tables WHERE table_name ='CUSTOM_WORKFLOW_ID_COUNTER';
   IF cnt <> 0 THEN
     EXECUTE IMMEDIATE 'DROP TABLE CUSTOM_WORKFLOW_ID_COUNTER';              
   END IF;       
 END;

两者都像魅力一样,但如果我尝试在一个SQL语句中执行:

  DECLARE cnt NUMBER;
  BEGIN
  SELECT COUNT(*) INTO cnt FROM user_tables WHERE table_name = 'CUSTOM_WORKFLOW_ID_COUNTER';
    IF cnt <> 0 THEN
      EXECUTE IMMEDIATE 'DROP TABLE CUSTOM_WORKFLOW_ID_COUNTER';              
    END IF;       
  END;
 create table CUSTOM_WORKFLOW_ID_COUNTER (
    id_ NUMBER (19) GENERATED by default on null as IDENTITY,
    type_ VARCHAR2(50 CHAR),
    masterId_ VARCHAR2(100 CHAR),
    counter_ NUMBER (19),
    primary key (id_)
    );

我明白了:

  

ORA-06550:Zeile 9,Spalte 1:   PLS-00103:Fand das符号“创建”   06550. 00000 - “行%s,列%s:\ n%s”   *原因:通常是PL / SQL编译错误。

如果我尝试在块内执行create语句,我会收到另一个错误:

  DECLARE cnt NUMBER;
  BEGIN
   SELECT COUNT(*) INTO cnt FROM user_tables WHERE table_name = 'CUSTOM_WORKFLOW_ID_COUNTER';
  IF cnt <> 0 THEN
   EXECUTE IMMEDIATE 'DROP TABLE CUSTOM_WORKFLOW_ID_COUNTER';
   EXECUTE IMMEDIATE 'create table CUSTOM_WORKFLOW_ID_COUNTER (
    id_ NUMBER (19) GENERATED by default on null as IDENTITY,
    type_ VARCHAR2(50 CHAR),
    masterId_ VARCHAR2(100 CHAR),
    counter_ NUMBER (19),
    primary key (id_)
    );';
 ELSE
    EXECUTE IMMEDIATE 'create table CUSTOM_WORKFLOW_ID_COUNTER (
    id_ NUMBER (19) GENERATED by default on null as IDENTITY,
    type_ VARCHAR2(50 CHAR),
    masterId_ VARCHAR2(100 CHAR),
    counter_ NUMBER (19),
    primary key (id_)
    );';      
  END IF;
END;

我收到此错误:

  

ORA-00922:Fehlendeoderungültige选项    ORA-06512:在Zeile 18中    00922. 00000 - “遗失或无效选项”

我做错了什么?

编辑: 删除分号后:

  DECLARE cnt NUMBER;
  BEGIN
   SELECT COUNT(*) INTO cnt FROM user_tables WHERE table_name = 'CUSTOM_WORKFLOW_ID_COUNTER';
 IF cnt <> 0 THEN
  EXECUTE IMMEDIATE 'DROP TABLE CUSTOM_WORKFLOW_ID_COUNTER';
  EXECUTE IMMEDIATE 'create table CUSTOM_WORKFLOW_ID_COUNTER (
id_ NUMBER (19) GENERATED by default on null as IDENTITY,
type_ VARCHAR2(50 CHAR),
masterId_ VARCHAR2(100 CHAR),
counter_ NUMBER (19),
primary key (id_)
)';
ELSE
EXECUTE IMMEDIATE 'create table CUSTOM_WORKFLOW_ID_COUNTER (
id_ NUMBER (19) GENERATED by default on null as IDENTITY,
type_ VARCHAR2(50 CHAR),
masterId_ VARCHAR2(100 CHAR),
counter_ NUMBER (19),
primary key (id_)
)';      
 END IF;
END;

我收到另一个错误;)

  

ORA-00054:Ressource belegt und Anforderung mit NOWAIT angegeben oder Timeout abgelaufen   ORA-06512:在Zeile 5中   00054. 00000 - “资源繁忙,并在指定NOWAIT或超时到期时获取”   *原因:感兴趣的资源很忙。   *操作:必要时重试或增加超时。

但这似乎是另一个问题。

2 个答案:

答案 0 :(得分:1)

只需删除create table末尾的分号:

  DECLARE cnt NUMBER;
  BEGIN
   SELECT COUNT(*) INTO cnt FROM user_tables WHERE table_name = 'CUSTOM_WORKFLOW_ID_COUNTER';
  IF cnt <> 0 THEN
   EXECUTE IMMEDIATE 'DROP TABLE CUSTOM_WORKFLOW_ID_COUNTER';
   EXECUTE IMMEDIATE 'create table CUSTOM_WORKFLOW_ID_COUNTER (
    id_ NUMBER (19) GENERATED by default on null as IDENTITY,
    type_ VARCHAR2(50 CHAR),
    masterId_ VARCHAR2(100 CHAR),
    counter_ NUMBER (19),
    primary key (id_)
    )';
 ELSE
    EXECUTE IMMEDIATE 'create table CUSTOM_WORKFLOW_ID_COUNTER (
    id_ NUMBER (19) GENERATED by default on null as IDENTITY,
    type_ VARCHAR2(50 CHAR),
    masterId_ VARCHAR2(100 CHAR),
    counter_ NUMBER (19),
    primary key (id_)
    )';      
  END IF;
END;

答案 1 :(得分:1)

问题是由于分号为黄色(第12行和第20行):

EXECUTE IMMEDIATE 'create table CUSTOM_WORKFLOW_ID_COUNTER (
id_ NUMBER (19) GENERATED by default on null as IDENTITY,
type_ VARCHAR2(50 CHAR),
masterId_ VARCHAR2(100 CHAR),
counter_ NUMBER (19),
primary key (id_)
)
  

'