[英]ORA-06550 line 2, column 44:PLS-00103: Encountered the symbol "end-of-file"
I am trying to execute a SQL script from Java using some rxjava2 methods.我正在尝试使用一些 rxjava2 方法从 Java 执行 SQL 脚本。 I have a resource file called
init_db.sql
.我有一个名为
init_db.sql
的资源文件。
Its content:其内容:
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE animals';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -942 THEN
RAISE;
END IF;
END;
/
CREATE TABLE animals (
id NUMBER GENERATED ALWAYS as IDENTITY(START with 1 INCREMENT by 1),
name VARCHAR2(40) NOT NULL,
amount INT,
CONSTRAINT animals_pk PRIMARY KEY (id));
INSERT INTO animals (name, amount) values ('Cats', 10);
INSERT INTO animals (name, amount) values ('Dogs', 10);
INSERT INTO animals (name, amount) values ('Rats', 10);
The method where the Java code is executed is the next:执行 Java 代码的方法是下一个:
public Completable initOracleDatabase(Vertx vertx, JDBCClient jdbc) {
System.out.println("Init Oracle Database **** executing..sql script...");
Completable completable =
jdbc.rxGetConnection().flatMapCompletable(sqlConnection -> vertx.fileSystem().rxReadFile("init_db.sql")
.flatMapObservable(buffer -> Observable.fromArray(buffer.toString().replaceAll(";.*$", "").split(";")))
.flatMapCompletable(sqlConnection::rxExecute)
.doAfterTerminate(sqlConnection::close));
return completable;
And this is the error that I got from the logs:这是我从日志中得到的错误:
java.sql.SQLException: ORA-06550: line 2, column 44:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
* & = - + ; < / > at in is mod remainder not rem return
returning <an exponent (**)> <> or != or ~= >= <= <> and or
like like2 like4 likec between into using || multiset bulk
member submultiset
and the error trace log:和错误跟踪日志:
at oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:509)
at oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:461)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1104)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:553)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:269)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:655)
at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:229)
at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:41)
at oracle.jdbc.driver.T4CStatement.executeForRows(T4CStatement.java:928)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1205)
at oracle.jdbc.driver.OracleStatement.executeInternal(OracleStatement.java:1823)
at oracle.jdbc.driver.OracleStatement.execute(OracleStatement.java:1778)
at oracle.jdbc.driver.OracleStatementWrapper.execute(OracleStatementWrapper.java:303)
Basically I would like to know if can I use in one only statement this SQL scriptor not?基本上我想知道我是否可以在一个唯一的声明中使用这个 SQL 脚本程序? or if I am missing anything or some error code translate between SQL script and Java?
或者如果我遗漏了任何东西或在 SQL 脚本和 Java 之间转换的错误代码?
You are mixing PL/SQL and SQL code in the same script.您在同一脚本中混合使用 PL/SQL 和 SQL 代码。
;
;
终止. /
on a newline and will contain nested PL/SQL (or SQL) statements that are each terminated by ;
/
终止,并将包含嵌套的 PL/SQL(或 SQL)语句,每个语句以;
终止。 . So you cannot naively split your code on ;
所以你不能天真地拆分你的代码
;
and expect it to work as you will incorrectly split the PL/SQL blocks.并期望它能正常工作,因为您会错误地拆分 PL/SQL 块。
In addition to using ;
除了使用
;
as an SQL statement terminator, you can also use /
on a newline;作为 SQL 语句终止符,您还可以在换行符上使用
/
; so you could rewrite your script as:所以你可以将你的脚本重写为:
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE animals';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -942 THEN
RAISE;
END IF;
END;
/
CREATE TABLE animals (
id NUMBER GENERATED ALWAYS as IDENTITY(START with 1 INCREMENT by 1),
name VARCHAR2(40) NOT NULL,
amount INT,
CONSTRAINT animals_pk PRIMARY KEY (id)
)
/
INSERT INTO animals (name, amount) values ('Cats', 10)
/
INSERT INTO animals (name, amount) values ('Dogs', 10)
/
INSERT INTO animals (name, amount) values ('Rats', 10)
/
And split the script on \n/\n
.并在
\n/\n
上拆分脚本。
Or, you can just ignore catching the ORA-00942 exception and just use SQL and continue using the ;
或者,您可以忽略捕获 ORA-00942 异常并仅使用 SQL 并继续使用
;
statement terminator:语句终止符:
DROP TABLE animals;
CREATE TABLE animals (
id NUMBER GENERATED ALWAYS as IDENTITY(START with 1 INCREMENT by 1),
name VARCHAR2(40) NOT NULL,
amount INT,
CONSTRAINT animals_pk PRIMARY KEY (id)
);
INSERT INTO animals (name, amount) values ('Cats', 10);
INSERT INTO animals (name, amount) values ('Dogs', 10);
INSERT INTO animals (name, amount) values ('Rats', 10);
Or, you can create a more complicated solution that actually parses PL/SQL and SQL statements and can split the script into individual statement based on the syntax tree.或者,您可以创建一个更复杂的解决方案,它实际解析 PL/SQL 和 SQL 语句,并可以根据语法树将脚本拆分为单独的语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.