ORACLE中判断表是否存在再删除表避免报错与MySql和SqlServer的不同

来源:互联网 发布:in美图软件 编辑:程序博客网 时间:2024/05/23 11:51

不同数据库中drop a table if it exists的不同:

In MySQL it is pretty easy to drop a table if it exists already. In Oracle and Microsoft’s SQL Server it is a little more complicated. Today I want to present you the solutions for these two DBMS’.
MySQL:

DROP TABLE IF EXISTS [table_name]

Oracle:

BEGIN

    EXECUTE IMMEDIATE 'DROP TABLE [table_name]';

    EXCEPTION WHEN OTHERS THEN NULL;

END;

SQL Server:

IF EXISTS (

    SELECT  TABLE_NAME FROM INFORMATION_SCHEMA.TABLES

    WHERE   TABLE_NAME = '[table_name]')

DROP TABLE  [table_name]

ORACLE中先判断表是否存在再新建表一例:

declare v_exists number;begin   --1、任务类型 TASK_TYPE_CD 建表...    select count(*) into v_exists from user_tables where table_name = 'EDW_T99_TASK_TYPE_CD';    if v_exists > 0 then    execute immediate 'drop table EDW_T99_TASK_TYPE_CD';    end if;    execute immediate '    create table EDW_T99_TASK_TYPE_CD    (      CODE_CD   VARCHAR2(20) PRIMARY KEY,      CODE_DESC VARCHAR2(100)    )';    execute immediate 'comment on table EDW_T99_TASK_TYPE_CD is ''任务类型''';    execute immediate 'comment on column EDW_T99_TASK_TYPE_CD.CODE_CD is ''代码''';    execute immediate 'comment on column EDW_T99_TASK_TYPE_CD.CODE_DESC is ''代码描述''';   --2、买入产品代码 BUY_TYPE_CD 建表...    select count(*) into v_exists from user_tables where table_name = 'EDW_T99_BUY_TYPE_CD';    if v_exists > 0 then    execute immediate 'drop table EDW_T99_BUY_TYPE_CD';    end if;    execute immediate '    create table EDW_T99_BUY_TYPE_CD    (      CODE_CD   VARCHAR2(20) PRIMARY KEY,      CODE_DESC VARCHAR2(100)    )';    execute immediate 'comment on table EDW_T99_BUY_TYPE_CD is ''买入产品代码''';    execute immediate 'comment on column EDW_T99_BUY_TYPE_CD.CODE_CD is ''代码''';    execute immediate 'comment on column EDW_T99_BUY_TYPE_CD.CODE_DESC is ''代码描述''';   end;/


转载地址:http://blog.csdn.net/nsj820/article/details/6308420

原创粉丝点击