ORACLE 创建表前判断是否已存在
Posted Chr☆s Kwok 的技术笔记
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ORACLE 创建表前判断是否已存在相关的知识,希望对你有一定的参考价值。
1、判断是否用户数据表已存在,不存在则创建:
DECLARE v_cnt number;
BEGIN
SELECT count(1) INTO v_cnt FROM dba_tables WHERE owner=\'ACT\' AND TABLE_NAME=\'PHYSICALORDERPRESCRIPTIONMAP\' ;
IF v_cnt = 0 THEN
EXECUTE immediate
\'CREATE TABLE "ACT"."PHYSICALORDERPRESCRIPTIONMAP"
( "PHYSICALREGISTERID" NUMBER(10,0) NOT NULL ENABLE,
"PHYSICALREGISTERITEMID" NUMBER(10,0) NOT NULL ENABLE,
"ENCOUNTERID" NUMBER(10,0) NOT NULL ENABLE,
"ORDERPRESCRIPTIONID" NUMBER(10,0) NOT NULL ENABLE,
"AMOUNT" NUMBER(18,4),
"ATTACHAMOUNT" NUMBER(18,4),
"ISDELETED" NUMBER(1,0) DEFAULT 0,
"ROWVERSION" TIMESTAMP (6) DEFAULT systimestamp,
"PHYSICALPARTYEXAMLABELID" NUMBER(10,0),
"PHYSICALITEMCLASSIFICATIONFLAG" NUMBER(3,0) DEFAULT 0,
"COMPOSITEITEMID" NUMBER(10,0),
"PHYSICALREGISTERBALANCEID" NUMBER(10,0),
"PEREGISTERDATE" DATE,
"PESEQNO" NUMBER(10,0),
"STATUSCODEID" NUMBER(10,0) DEFAULT (0),
CONSTRAINT "PHYSICALORSMAP_PK" PRIMARY KEY ("PHYSICALREGISTERID", "PHYSICALREGISTERITEMID", "ENCOUNTERID", "ORDERPRESCRIPTIONID"))\';
END if;
end;
/
2、判断是否全局临时表已存在,不存在则创建:
DECLARE v_cnt number;
BEGIN
SELECT count(1) INTO v_cnt FROM dba_tables WHERE owner=\'ACT\' AND TABLE_NAME=\'TEMPPHYSICALOPACCOUNTLIST\' ;
IF v_cnt = 0 THEN
EXECUTE immediate
\'CREATE GLOBAL TEMPORARY TABLE "ACT"."TEMPPHYSICALOPACCOUNTLIST"
( "ACCOUNTLISTID" NUMBER(10,0),
"ORDERPRESCRIPTIONID" NUMBER(10,0),
"PARENTORDERPRESCRIPTIONID" NUMBER(10,0),
"ENCOUNTERID" NUMBER(10,0),
"ACCOUNTINGORGANIZATIONID" NUMBER(10,0),
"COSTORGANIZATIONID" NUMBER(10,0),
"ACCOUNTINGEMPLOYEEID" NUMBER(10,0),
"ACCOUNTINGDATETIME" DATE,
"OCCURDATE" DATE,
"APPENDDATETIME" DATE,
"EXECORGANIZATIONID" NUMBER(10,0),
"ACCOUNTINGDOCTORID" NUMBER(10,0),
"ORDERSOURCEFLAG" NUMBER(3,0),
"MAINCONSUMABLECODEID" NUMBER(10,0),
"MAINCONSUMABLEID" NUMBER(10,0),
"CONSUMABLECODEID" NUMBER(10,0),
"CONSUMABLEID" NUMBER(10,0),
"PRICE" NUMBER(18,4),
"QUANTITY" NUMBER(18,4),
"AMOUNT" NUMBER(18,4),
"FEEKINDID" NUMBER(10,0) DEFAULT -1,
"PAYPROPORTION" NUMBER(18,4) DEFAULT 10000,
"HASPAID" NUMBER(1,0) DEFAULT 0,
"ISFEEKINDFROMCALC" NUMBER(1,0) DEFAULT 0,
"STANDARDPRICE" NUMBER(18,4),
"ISDUPLICATEREMOVAL" NUMBER(1,0) DEFAULT 0,
"PHYSICALCLASSIFICATIONFLAG" NUMBER(10,0) DEFAULT 1,
"PHYSICALITEMCLASSIFICATIONFLAG" NUMBER(10,0) DEFAULT 0 ) ON COMMIT DELETE ROWS\';
END if;
end;
/
DECLARE
v_cnt number(10,0);
BEGIN
SELECT count(1) INTO v_cnt FROM dba_tab_columns c WHERE c.owner=\'ACT\' AND TABLE_NAME=\'TEMPPHYSICALOPACCOUNTLIST\' AND c.column_name = \'ISDUPLICATEREMOVAL1\';
IF (v_cnt = 0) THEN
EXECUTE immediate \'ALTER TABLE "ACT"."TEMPPHYSICALOPACCOUNTLIST" ADD "ISDUPLICATEREMOVAL1" NUMBER(1,0) DEFAULT 0\';
END if;
END;
/
DECLARE
v_cnt number(10,0);
BEGIN
SELECT count(1) INTO v_cnt FROM dba_tab_columns c WHERE c.owner=\'ACT\' AND TABLE_NAME=\'TEMPPHYSICALOPACCOUNTLIST\' AND c.column_name = \'PHYSICALCLASSIFICATIONFLAG\';
IF (v_cnt = 0) THEN
EXECUTE immediate \'ALTER TABLE "ACT"."TEMPPHYSICALOPACCOUNTLIST" ADD "PHYSICALCLASSIFICATIONFLAG" NUMBER(10,0) DEFAULT 1\';
END if;
END;
/
DECLARE
v_cnt number(10,0);
BEGIN
SELECT count(1) INTO v_cnt FROM dba_tab_columns c WHERE c.owner=\'ACT\' AND TABLE_NAME=\'TEMPPHYSICALOPACCOUNTLIST\' AND c.column_name = \'PHYSICALITEMCLASSIFICATIONFLAG\';
IF (v_cnt = 0) THEN
EXECUTE immediate \'ALTER TABLE "ACT"."TEMPPHYSICALOPACCOUNTLIST" ADD "PHYSICALITEMCLASSIFICATIONFLAG" NUMBER(10,0) DEFAULT 0\';
END if;
END;
/
Oracle SQL示例
创建表:
1 --删除表: 2 --创建表前删除已存在的同名表,确保能够创建成功。 3 --但是这种操作非常危险,万一删除了不该删的表,会非常糟糕。 4 Drop table T_emp; 5 --创建表: 6 create table T_emp 7 ( 8 empno number not null, 9 empname varchar(20) not null, 10 gender nvarchar(1) not null, 11 age number not null, 12 createtime date default sysdate, 13 isdelete char(1) default(‘N‘) not null, 14 --创建约束(主键约束,唯一约束,check约束) 15 constraint pk_empno primary key(empno), 16 constraint u_empname unique(empname), 17 constraint c_isdelete check(isdelete in(‘Y‘,‘N‘) 18 );
插入记录:
1 /*插入记录*/ 2 3 --完整填写 4 insert into T_emp 5 ( 6 empno, 7 empname, 8 gender, 9 age. 10 createtime, 11 isdelete 12 ) 13 values 14 ( 15 ‘x1234567‘, 16 ‘关云长‘, 17 ‘男‘, 18 ‘24‘, 19 sysdate, 20 ‘N‘ 21 ); 22 23 --仅填写两个不可重复的字段 24 insert into T_emp 25 ( 26 empno, 27 empname 28 ) 29 values 30 ( 31 ‘x1234568‘, 32 ‘赵子龙‘ 33 );
删除记录:
1 --删除记录 2 delete from t_emp 3 where empno=‘x1234567‘;
更新记录:
1 update t_emp 2 set empno=‘x7654321‘ 3 where empno=‘x1234567‘;
查询记录:
1 select empno,empname 2 from t_emp 3 where rownum<=2;
以上是关于ORACLE 创建表前判断是否已存在的主要内容,如果未能解决你的问题,请参考以下文章
oracle创建表之前判断表是不是存在,如果存在则删除已有表
oracle创建表之前判断表是不是存在,如果存在则删除已有表