executeSqlScript因Spring for PL / SQL块而失败

时间:2015-06-10 10:37:16

标签: 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文件中表达什么? 错误重现的原因是什么?

1 个答案:

答案 0 :(得分:3)

您似乎正在尝试在脚本中使用PL / SQL的功能。

executeSqlScript(..)中的AbstractTransactionalJUnit4SpringContextTests方法在幕后内部委托给ScriptUtils.executeSqlScript(..)ScriptUtils仅支持纯SQL脚本。

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

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