HSQL - 意外的令牌:声明中的PROCEDURE

时间:2014-07-03 14:49:40

标签: hsqldb

我正在尝试转换存储过程以在HSQL DB中使用以进行集成测试。这里的SQL基本就足够了 - 我们只是返回一组虚拟数据

CREATE PROCEDURE policy_Attribution(p_valDate Date) 
    MODIFIES SQL DATA
    BEGIN ATOMIC
    CREATE TABLE #retvals ( 
        Policy_ID INT, 
        Attribution_Name CHAR(64), 
        Attribution_Type CHAR(20), 
        Risk CHAR(20), 
        Revenue FLOAT, 
        Claims FLOAT 
    )
    INSERT INTO #retvals VALUES (1, 'name', 'type', 'risk', 1.5, 1.6)
    INSERT INTO #retvals VALUES (2, 'name2', 'type2', 'risk2', 0.5, 0.6) 
    INSERT INTO #retvals VALUES (3, 'name3', 'type3', 'risk3', 2.5, 2.6)
    SELECT * FROM #retvals
END

当我运行我的测试时,由于数据库初始化时出现此错误

Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement at line 19 of resource class path resource [hsql/attribution-hsqldb.sql]:  CREATE PROCEDURE policy_Attribution(p_valDate Date)   MODIFIES SQL DATA  BEGIN ATOMIC  CREATE TABLE #retvals (    Policy_ID INT,    Attribution_Name CHAR(64),    Attribution_Type CHAR(20),    Risk CHAR(20),    Revenue FLOAT,    Claims FLOAT   )  INSERT INTO #retvals VALUES (1, 'name', 'type', 'risk', 1.5, 1.6)  INSERT INTO #retvals VALUES (2, 'name2', 'type2', 'risk2', 0.5, 0.6)   INSERT INTO #retvals VALUES (3, 'name3', 'type3', 'risk3', 2.5, 2.6)  SELECT * FROM #retvals END
    at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.executeSqlScript(ResourceDatabasePopulator.java:199)
    at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:132)
    at org.springframework.jdbc.datasource.init.CompositeDatabasePopulator.populate(CompositeDatabasePopulator.java:55)
    at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:47)
    ... 41 more
Caused by: java.sql.SQLException: Unexpected token: PROCEDURE in statement [ CREATE PROCEDURE]
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    at org.hsqldb.jdbc.jdbcStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.jdbcStatement.execute(Unknown Source)
    at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.executeSqlScript(ResourceDatabasePopulator.java:184)
    ... 44 more

hsqldb抱怨的意外标记是什么?

2 个答案:

答案 0 :(得分:2)

应使用DECLARE关键字创建表。还需要进行其他更改:

CREATE PROCEDURE policy_Attribution(p_valDate Date) 
    MODIFIES SQL DATA
    BEGIN ATOMIC
    DECLARE TABLE retvals ( 
        Policy_ID INT, 
        Attribution_Name CHAR(64), 
        Attribution_Type CHAR(20), 
        Risk CHAR(20), 
        Revenue FLOAT, 
        Claims FLOAT 
    );
    BEGIN

    INSERT INTO retvals VALUES (1, 'name', 'type', 'risk', 1.5, 1.6);
    INSERT INTO retvals VALUES (2, 'name2', 'type2', 'risk2', 0.5, 0.6); 
    INSERT INTO retvals VALUES (3, 'name3', 'type3', 'risk3', 2.5, 2.6);
    DECLARE result CURSOR FOR SELECT * FROM retvals;
    OPEN result;
    END;
END

参见指南:

http://hsqldb.org/doc/2.0/guide/sqlroutines-chapt.html#src_psm_table_vars

答案 1 :(得分:0)

也许丢失的分号是问题所在。尝试在每个SQL语句的末尾添加它们。