如何修复Oracle SQL中的“名称已被使用”错误?

时间:2019-04-13 01:02:01

标签: sql oracle ddl

如何解决Oracle SQL中的“名称已被使用”错误?

CREATE TABLE permission (
    user_id           INTEGER NOT NULL,
    device_id         VARCHAR2(20) NOT NULL,
    permission_type   VARCHAR2(20),
    permission_id     INTEGER NOT NULL
)
LOGGING;

ALTER TABLE permission ADD CONSTRAINT permission_pk PRIMARY KEY ( permission_id );

我遇到以下错误,

 Error report -
    ORA-00955: name is already used by an existing object
    00955. 00000 -  "name is already used by an existing object"
    *Cause:    
    *Action:

1 个答案:

答案 0 :(得分:0)

您可以更改代码。

   declare 
    duplicate_table EXCEPTION;
    PRAGMA EXCEPTION_INIT (duplicate_table , -955);
    begin

    execute  immediate 'CREATE TABLE permission (
        user_id           INTEGER NOT NULL,
        device_id         VARCHAR2(20) NOT NULL,
        permission_type   VARCHAR2(20),
        permission_id     INTEGER NOT NULL
    )
    LOGGING' ;

    exception
     WHEN duplicate_table  THEN
        dbms_output.put_line('Duplicate table');
        --null;

    end;


SQL*Plus: Release 12.2.0.1.0 Production on Mon Apr 15 10:10:01 2019

Copyright (c) 1982, 2017, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Release 11.2.0.3.0 - 64bit Production

SQL> set serveroutput on;
SQL> declare
  2  duplicate_table EXCEPTION;
  3  PRAGMA EXCEPTION_INIT (duplicate_table , -955);
  4  begin
  5
  6  execute  immediate 'CREATE TABLE permission (
  7      user_id           INTEGER NOT NULL,
  8      device_id         VARCHAR2(20) NOT NULL,
  9      permission_type   VARCHAR2(20),
 10      permission_id     INTEGER NOT NULL
 11  )
 12  LOGGING' ;
 13
 14  exception
 15   WHEN duplicate_table  THEN
 16      dbms_output.put_line('Duplicate table');
 17      --null;
 18
 19  end;
 20  /

Duplicate table

PL/SQL procedure successfully completed.