简体   繁体   English

org.h2.jdbc.JdbcSQLException:未知数据类型:“DATEADD”; SQL 声明

[英]org.h2.jdbc.JdbcSQLException: Unknown data type: "DATEADD"; SQL statement

I'm using h2 to inject the db during run time and testing, everything was working fine until I started trying to make the date field current, not hard-coded.我在运行时和测试期间使用 h2 注入数据库,一切正常,直到我开始尝试使日期字段成为当前字段,而不是硬编码。 I thought it was related to the jdbc version I've and updated spring jdbc library to be the latest, but that didn't solve the problem.我以为是和我的jdbc版本有关,更新了spring jdbc库为最新,但并没有解决问题。

This is the code I'm using to inject the data both on run-time and testing:这是我用来在运行时和测试中注入数据的代码:

This code runs during run-time, and it works perfectly before I tried to make the date current.此代码在运行时运行,并且在我尝试将日期设置为当前日期之前它运行良好。

        ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
        resourceDatabasePopulator.addScript(new ClassPathResource("schema.sql"));
        resourceDatabasePopulator.addScript(new ClassPathResource("data.sql"));
        DatabasePopulatorUtils.execute(resourceDatabasePopulator, dataSource); // This is what the DataSourceInitializer does.


For testing purpose, I'm using this code, as I mentioned, it was working perfectly before I tried to make the date current.出于测试目的,我正在使用这段代码,正如我所提到的,在我尝试将日期设置为当前日期之前,它运行良好。

        DataSource dataSource = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
                .addScript("classpath:schema.sql")
                .addScript("classpath:data.sql")
                .build();

data.sql file data.sql 文件

INSERT INTO  TABLE_X (
  dayxx,
  xxx
) VALUES
(CONVERT(char(50), DATEADD('DAY', -1,CURRENT_DATE()),126),'xxx')


Exception例外

Caused by: org.h2.jdbc.JdbcSQLNonTransientException: Unknown data type: "DATEADD"; SQL statement:
INSERT INTO TABLE_X ( dayxx, xxx ) VALUES (CONVERT(char(50), DATEADD('DAY', -1,CURRENT_DATE()),126),'XXX') [50004-200]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:505)
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:429)
    at org.h2.message.DbException.get(DbException.java:205)
    at org.h2.message.DbException.get(DbException.java:181)
    at org.h2.command.Parser.parseColumnWithType(Parser.java:5971)
    at org.h2.command.Parser.readFunctionParameters(Parser.java:3793)
    at org.h2.command.Parser.readFunction(Parser.java:3772)
    at org.h2.command.Parser.readTerm(Parser.java:4305)
    at org.h2.command.Parser.readFactor(Parser.java:3343)
    at org.h2.command.Parser.readSum(Parser.java:3330)
    at org.h2.command.Parser.readConcat(Parser.java:3305)
    at org.h2.command.Parser.readCondition(Parser.java:3108)
    at org.h2.command.Parser.readExpression(Parser.java:3059)
    at org.h2.command.Parser.parseValuesForCommand(Parser.java:1877)
    at org.h2.command.Parser.parseInsertGivenTable(Parser.java:1817)
    at org.h2.command.Parser.parseInsert(Parser.java:1749)
    at org.h2.command.Parser.parsePrepared(Parser.java:954)
    at org.h2.command.Parser.parse(Parser.java:843)
    at org.h2.command.Parser.parse(Parser.java:815)
    at org.h2.command.Parser.prepareCommand(Parser.java:738)
    at org.h2.engine.Session.prepareLocal(Session.java:657)
    at org.h2.engine.Session.prepareCommand(Session.java:595)
    at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1235)
    at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:212)
    at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:201)
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:472)
    ... 53 more



As reported in the docs , Convert function looks like this:文档中所述,Convert function 如下所示:

CONVERT ( value, dataType )转换(值,数据类型)

Converts a value to another data type.将值转换为另一种数据类型。

Example:例子:

CONVERT(NAME, INT)转换(名称,整数)

Since you're passing既然你路过

CONVERT(char(50), DATEADD('DAY', -1,CURRENT_DATE()),126) CONVERT(char(50), DATEADD('DAY', -1,CURRENT_DATE()),126)

The error states that DATEADD isn't a valid data type, since it's the value, not the type, so try using the right syntax.该错误表明 DATEADD 不是有效的数据类型,因为它是值,而不是类型,因此请尝试使用正确的语法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.h2.jdbc.JdbcSQLException:未找到表“ PACCHETTIVISITETURISTICHE”; SQL语句: - org.h2.jdbc.JdbcSQLException: Table “PACCHETTIVISITETURISTICHE” not found; SQL statement: org.h2.jdbc.JdbcSQLException:找不到表 - org.h2.jdbc.JdbcSQLException: Table not found org.h2.jdbc.JdbcSQLException:找不到函数“ TO_CHAR” - org.h2.jdbc.JdbcSQLException: Function “TO_CHAR” not found org.h2.jdbc.JdbcSQLDataException:数据转换错误转换“'MONTH'”; SQL 声明: - org.h2.jdbc.JdbcSQLDataException: Data conversion error converting “'MONTH'”; SQL statement: org.h2.jdbc.JdbcSQLSyntaxErrorException:SQL 语句中的语法错误 - org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement SQL更改DATEADD数据类型 - SQL change DATEADD data type org.h2.jdbc.JdbcSQLSyntaxErrorException:SQL 语句“CREATE[*] SCHEMA DATABASE”中的语法错误; SQL语句: - org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "CREATE[*] SCHEMA DATABASE "; SQL statement: Spring SQL:org.h2.jdbc.JdbcSQLSyntaxErrorException:SQL 语句中的语法错误“;预期为“标识符”,使用 INSERT INTO 时 - Spring SQL: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "; expected "identifier", when using INSERT INTO 未知数据类型:“USER”; SQL 声明 - Unknown data type: “USER”; SQL statement 引起:org.h2.jdbc.JdbcSQLSyntaxErrorException:Function “SYSUTCDATETIME”未找到; SQL 声明:[90022-200] - Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Function "SYSUTCDATETIME" not found; SQL statement:[90022-200]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM