【发布时间】:2026-02-06 19:35:02
【问题描述】:
我正在做一个项目,我必须使用 Oracle Database 12c,并且我必须手动编写所有查询(所以我不能使用 Spring Data)。 对于创建所有表和关系,我使用 schema.sql,对于模板数据,我使用 data.sql。
我在检查表或数据是否已经存在时遇到了问题。 在 MySQL 中创建表就像“如果不存在则创建表”。 不幸的是,在 PL/SQL 中,没有“如果不存在”的等价物。我将此功能替换为:
begin
execute immediate
'CREATE TABLE user_data (
some data
)';
exception when others then if SQLCODE = -955 then null; else raise; end if;
end;
当我在 SQL Developer 或 Intellij 的 SQL 控制台中运行此脚本时它可以工作,但是当我想运行应用程序并且 Spring Boot 尝试从 schema.sql 执行脚本时会出现问题。
终端的输出告诉我们:
nested exception is java.sql.SQLException: ORA-06550: line 8, column 4:
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
所以看起来 Spring Boot 不知道它应该在“开始”和“结束”之间运行语句。 知道如何管理数据库初始化问题吗?
作为一种解决方法,我可以在每次运行应用程序时删除表,但这不是最佳解决方案(当有人第一次运行应用程序时它不会工作)。
【问题讨论】:
-
查看 Liquidbase 或 Flyway。有专门用于此的库
-
'CREATE TABLE user_data ( some data它应该有一些列而不是一些数据 -
@pvpkiran,很遗憾,我不能使用 Liquidbase 或 Flyway 之类的库...
-
@XING 这只是一个例子。在我的应用程序中,我有一些列,但没有必要在此处显示它们,因为问题与它们无关
标签: java oracle spring-boot plsql oracle12c