Spring Boot schema.sql,使用PL / SQL初始化数据库

问题描述 投票:1回答:1

我正在开发一个项目,我必须使用Oracle Database 12c,我必须手动编写所有查询(所以我不能使用Spring Data)。为了创建所有表和关系,我使用schema.sql,对于模板数据,我使用data.sql。

我在检查表或数据是否已存在时遇到问题。在MySQL中,创建表就像“如果不存在则创建表”。不幸的是,在PL / SQL中,没有“if not exists”的等价物。我将此功能替换为:

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不知道它应该在“begin”和“end”之间运行语句。任何想法如何管理数据库初始化的问题?

作为一种解决方法,我可以在运行每个应用程序时删除表,但它不是最佳解决方案(当有人第一次运行应用程序时它不起作用)。

java oracle spring-boot plsql oracle12c
1个回答
1
投票

首先,我想分享两个似乎与此问题相关的主题:

在那里你会找到一个应该有效的解决方案:创建一个存储过程并在你的schema.sql语句中使用

call recreate_table('USER_DATA','CREATE TABLE USER_DATA (SOME DATA)');  

CALL语句广泛用于不同的数据库,缩写为只有一个分号的语句,因此效果很好。

其次,我可能只想,主要的问题是PL / SQL中的匿名块(以及可能包含多个分号的其他复杂的语句)应该由/字符完成。我建议你尝试将这个字符附加到脚本的末尾,查看thisthis的答案,如果它不起作用,请创建一个存储过程。

还要注意,还有另一种检查表存在的方法(通过等待异常模式):

select count(*)
  from user_tables t
 where t.table_name = 'USER_DATA'
   and rownum < 2
© www.soinside.com 2019 - 2024. All rights reserved.