if exists(select 1 from user_tables where table_name='表名') ...?
oracle这样是不允许的。适合sql server
试了下,还真不行。会报ora-06550错误 “函数或伪列exists只能在sql语句中使用”
你要是不嫌麻烦可以改成块的写法。 declare n_cnt number; begin select count(*) into n_cnt from user_tables where table_name='A'; if n_cnt>0 then execute immediate 'drop table "A"'; end if; execute immediate 'CREATE TABLE "A" ( "ID" VARCHAR2(10), "NAME" VARCHAR2(10) )'; end;
执行过程: Microsoft Windows [Version 5.2.3790] (C) Copyright 1985-2003 Microsoft Corp.C:\Documents and Settings\Administrator>sqlplus / as sysdbaSQL*Plus: Release 10.1.0.2.0 - Production on Thu Feb 9 11:25:59 2012Copyright (c) 1982, 2004, Oracle. All rights reserved. Connected to: Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Production With the Partitioning, OLAP and Data Mining optionsSQL> @c:\aa.txt 1PL/SQL procedure successfully completed.2PL/SQL procedure successfully completed.3PL/SQL procedure successfully completed.SQL>脚本内容: set serveroutput on declare n_cnt number; begin select count(*) into n_cnt from dual; dbms_output.put_line(1); end; /declare n_cnt number; begin select count(*) into n_cnt from dual; dbms_output.put_line(2); end; /declare n_cnt number; begin select count(*) into n_cnt from dual; dbms_output.put_line(3); end; /
可以用存储过程生成脚本,然后utl_file写到操作系统,然后调用执行
执行 效率那个好呢 块里面的 sql 存储过程里面都有 相比用存储过程建表 有什么好处吗
declare 在 plsql里面那个文件夹能看到呢 比如想继续改 继续写
declare begin exception end 是最基本的plsql单元。 像存储过程,包,函数,触发器等等都是基于这个最基础的单元的。 只不过那些有名字。
如果要想实现,就必须写PL/SQL程序。
表不存在 drop 就报错 提示表不存在
if exists(select 1 from user_tables where table_name='表名')
...?
“函数或伪列exists只能在sql语句中使用”
declare
n_cnt number;
begin
select count(*) into n_cnt from user_tables where table_name='A';
if n_cnt>0 then
execute immediate 'drop table "A"';
end if;
execute immediate 'CREATE TABLE "A"
( "ID" VARCHAR2(10),
"NAME" VARCHAR2(10)
)';
end;
写在块里面 sqlplus 执行的时候 不能在一个脚本里面 执行 多个块 因为表有几百张 exists能用在建表时候判断是否 存在呢 要建几百张表 有没有其他的办法 先判断在执行 一定要用sqlplus 执行sql语句文件呢?谢谢
Microsoft Windows [Version 5.2.3790]
(C) Copyright 1985-2003 Microsoft Corp.C:\Documents and Settings\Administrator>sqlplus / as sysdbaSQL*Plus: Release 10.1.0.2.0 - Production on Thu Feb 9 11:25:59 2012Copyright (c) 1982, 2004, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Production
With the Partitioning, OLAP and Data Mining optionsSQL> @c:\aa.txt
1PL/SQL procedure successfully completed.2PL/SQL procedure successfully completed.3PL/SQL procedure successfully completed.SQL>脚本内容:
set serveroutput on declare
n_cnt number;
begin
select count(*) into n_cnt from dual;
dbms_output.put_line(1);
end;
/declare
n_cnt number;
begin
select count(*) into n_cnt from dual;
dbms_output.put_line(2);
end;
/declare
n_cnt number;
begin
select count(*) into n_cnt from dual;
dbms_output.put_line(3);
end;
/
执行 效率那个好呢 块里面的 sql 存储过程里面都有 相比用存储过程建表 有什么好处吗
begin
exception
end
是最基本的plsql单元。
像存储过程,包,函数,触发器等等都是基于这个最基础的单元的。
只不过那些有名字。
从ALL_TABLE里面查找是否存在该名称的表格