我有一个小的SQL脚本,我正在使用Oracle的SQL * Plus来模拟表的创建或替换:
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE symbols';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -942 THEN
END IF;
END;
/
CREATE TABLE symbols (
blah blah,
blah blah,
);
EXIT;
SQL * Plus命令行是:
sqlplus aegsys15_owner/pass#234@MARVINUAT03 @createSymbolsTable.sql << EOF
> EOF
如果在END之后省略正斜杠(/),它似乎只处理第一个BEGIN / END块,并忽略下面的CREATE TABLE部分。此外,它根本不打印任何帮助 - 只需连接/断开连接:
SQL*Plus: Release 11.2.0.1.0 Production on Tue Sep 13 15:49:34 2011
Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
78 Disconnected from Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
但是,如果我有正斜杠,它会给我一个错误:
END IF;
*
ERROR at line 6:
ORA-06550: line 6, column 5:
PLS-00103: Encountered the symbol "END" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
CREATE TABLE symbols (
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
首先,将BEGIN / END异常块放在顶部,将CREATE TABLE块放在同一个.sql文件中的最佳方法是什么?
其次,从SQL * Plus中获取一些有用的输出是什么?我们运行的每个.sql文件可能有多个CREATE语句(表,索引,同义词等)。我们理想的输出将是:
TABLE foo: Pass
SYNONYM bar: Fail
INDEX foo_1: Pass
不确定是否可以使用SQL或PL / SQL实现类似的东西 - 很高兴为此编写一个Bash或Python包装器脚本,如果你们认为这是一个更好的解决方案。
干杯, 维克多
答案 0 :(得分:2)
你忘记输入你的if语句..
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE symbols';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -942 THEN
--here you have to write something for this exception
-- if you don't have any activity to do then you can use NULL (atleast)
-- you can't put this if statement body empty in oracle
NULL;
END IF;
END;
/
并且如果您在开始之前的第一行也使用declare
答案 1 :(得分:-1)
对于slqplus中的输出,请使用prompt命令。对于pl / sql的输出(即在开始/结束块内),使用dbms_output.put_line()函数。
prompt Creating foo table
begin
create table foo...;
dbms_output.put_line('success');
exception
when others then
dbms_output.put_line('fail');
end;
/