oracle中创建表时先判断是否存在表再drop的SQL
来源:互联网 发布:大学要考的证书 知乎 编辑:程序博客网 时间:2024/06/06 09:38
在写oracle的创建表的SQL时,为了SQL能够反复执行,一般都会在create前面加入drop表的语句,但这样先drop再create的语句在第一次执行时,会报一个不存在该表的错误,查了一下,oracle中没有像sybase那样先判断是否存在表再drop表的语句。
sybase中用以下语句就能轻松判断是否已经存在了某表:
if exists (select 1 from sysobjects where id = object_id('user_info') and type = 'U') drop table user_info go
或者
if object_id('xxx_view')is not null drop view xxx_view go
第一种方式中其中type为对象的类型,如果对象是view,刚type='V'
而oracle中,并没有这样方便的语句,然而我们难道就不能实现这个功能了吗?
可以利用存储过程来实现,例如以下语句:
declare num number; begin select count(1) into num from user_tables where table_name='user_info'; if num>0 then execute immediate 'drop table user_info'; end if; execute immediate 'create table user_info (user_code varchar2(10) not null, user_name varchar2(30), sex varchar2(1), constraint pk_user_info primary key (user_code))'; end; /
不过将这样的存储过程与正常的sql放在一起执行时,一定要加最后一行的“/”,以告诉编译器存储过程执行完毕,可以继续执行正常的SQL了,我就犯过这样的错误。
- oracle中创建表时先判断是否存在表再drop的SQL
- oracle中创建表时先判断是否存在表再drop的SQL
- oracle 创建表前判断是否存在
- Oracle中判断表是否存在!
- Oracle中判断表是否存在
- Oracle中判断表是否存在!
- Sql server中判断表是否存在
- 判断表中是否存在记录的SQL语句
- SQL 判断表中是否存在记录的高效率方法
- Oracle判断表是否存在的函数
- sql 判断临时表的是否存在
- sql 判断临时表的是否存在
- oracle创建表时,先判断表是否存在
- 【oracle】创建表前判断表是否存在
- Oracle 创建表,并且判断表是否存在
- Sql 判断是否存在某张表,没有创建
- [Oracle] 判断表是否存在
- oracle sql 判断字段是否存在
- How do I get my old emails from Windows 98/Outlook 2000 to Win...
- dojo对象方法
- dwr 介绍
- Re:Method does not have the same signature as delegate
- RS232/RS422/RS485间的区别
- oracle中创建表时先判断是否存在表再drop的SQL
- RS-232、RS-485、RS-422接线的临时的解决方案
- 在IE下监控页面内存资源和dom节点(sIEve软件使用简介)
- event关键字存在的必要
- Not able to wire events in user control
- dojo之ValidationTextbox
- C/C++中的函数参数传递机制(转)
- CFile与CStdioFile的区别
- php汉字转拼音程序