sql - executeSqlScript 因 Spring for PL/SQL block 而失败

标签 sql spring oracle11g spring-test

我正在尝试使用内置函数 executeSqlScript 填充我的数据库来自 AbstractTransactionalJUnit4SpringContextTests使用以下外部 SQL 文件。

declare
   id number;
begin
   insert into table1 (field1) values ('V1') returning account__id  into id;
   insert into table2 (my_id, field2) VALUES (id, 'Value3');
end;

但是我收到以下错误。我不确定我允许在我想使用 executeSqlScript 执行的 SQL 文件中做什么.
org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement at line 1 of resource class path resource [testdata.sql]: declare id number; nested exception is java.sql.SQLException: ORA-06550: line 1, column 17:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:

   := . ( @ % ; not null range default character
Caused by: java.sql.SQLException: ORA-06550: line 1, column 17:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:

   := . ( @ % ; not null range default character

所以我的问题是:
我可以在 executeSqlScript 的 SQL 文件中表达什么? ?
我收到错误的原因是什么?

最佳答案

您似乎正在尝试在脚本中使用 PL/SQL 的功能。
executeSqlScript(..) AbstractTransactionalJUnit4SpringContextTests 中的方法内部委托(delegate)给 ScriptUtils.executeSqlScript(..)幕后,和ScriptUtils仅支持纯 SQL 脚本。

因此,您可能需要切换到简单的 SQL 语句并找到一种不同的机制来检索 account__id 的值。来自 table1 .

另一种选择(我尝试过 而不是 )是将语句分隔符更改为 ";" 以外的其他内容。 (例如, "end;" ),但您不能通过 AbstractTransactionalJUnit4SpringContextTests.executeSqlScript 做到这一点.相反,您需要调用 ScriptUtils.executeSqlScript(..)或(也许最好)使用 ResourceDatabasePopulator .

关于sql - executeSqlScript 因 Spring for PL/SQL block 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30753901/

相关文章:

sql - 使用 WHERE 和 AS 从数据库中提取数据

python - Django 中 OneToMany 或 ManyToOne 的任何替代方案

mysql - UTC_TIMESTAMP() 是否受夏令时影响?

spring - Spring注解是如何工作的?

java - 传入 JMS 消息的事务同步未激活

mysql - 如何按列具有特定值的组求和

java - 无法使用 SpringSource Tool Suite 计算构建计划

java - 如何将空字符串更新到oracle Clob

sql - 使用 toChar 在 W3C XML Schema xs :dateTime type format 中输出日期

oracle - oracle pl/sql中如何选择成嵌套类型?