【问题标题】:Wrong SQL statements syntax in file data.sql in Spring Boot nad PostgreSQLSpring Boot 和 PostgreSQL 中文件 data.sql 中的 SQL 语句语法错误
【发布时间】:2018-03-11 21:42:55
【问题描述】:

我想创建一个将由 PostgreSQL 数据库创建的条件查询

IF ( NO EXISTS(SELECT * FROM users WHERE username = 'JonkiPro'))
BEGIN
  INSERT INTO UserEntity(id, username, email, password, enabled, registration_date, modified_date)
  VALUES(1, 'JonkiPro', 'someemail@someemail.com,', 'safsd', true, GetDate(), GetDate())
END

但是编译的时候出错了

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceInitializer': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of class path resource [db/init/data.sql]: IF ( NO EXISTS(SELECT * FROM users WHERE username = 'JonkiPro')); nested exception is org.postgresql.util.PSQLException: BŁĄD: błąd składni w lub blisko "IF"
      Pozycja: 1
        at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
 ...

    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1633) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
            ... 99 common frames omitted
        Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of class path resource [db/init/data.sql]: IF ( NO EXISTS(SELECT * FROM users WHERE username = 'JonkiPro')); nested exception is org.postgresql.util.PSQLException: BŁĄD: błąd składni w lub blisko "IF"
          Pozycja: 1
            at ...
            ... 114 common frames omitted
        Caused by: org.postgresql.util.PSQLException: ERROR: syntax error in or near "IF".
          Pozycja: 1
            at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
            at ...

我不知道他为什么看不懂IF的命令。但是,当我在DO $$ ... $$ 之间执行命令时,它会拒绝异常

      Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceInitializer': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.UncategorizedScriptException: Failed to execute database script from resource [class path resource [db/init/data.sql]]; nested exception is java.lang.ArrayIndexOutOfBoundsException
     ...
            ... 99 common frames omitted
        Caused by: org.springframework.jdbc.datasource.init.UncategorizedScriptException: Failed to execute database script from resource [class path resource [db/init/data.sql]]; nested exception is java.lang.ArrayIndexOutOfBoundsException
...

    org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:470) ~[spring-jdbc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
                ... 127 common frames omitted

我的第二个问题是如何在块BEGIN ... END中添加一个命令将数据输入到另一个表中

【问题讨论】:

    标签: java sql spring postgresql spring-mvc


    【解决方案1】:

    请参阅this question 了解有条件地创建用户的替代方法,因为如果不先加载 PL/pgSQL 扩展,您似乎无法使用 IF 语句。在您的情况下,这将导致如下查询:

    INSERT INTO UserEntity(id, username, email, password, enabled, registration_date, modified_date)
    SELECT
        1, 'JonkiPro', 'someemail@someemail.com,', 'safsd', true, GetDate(), GetDate()
    WHERE NOT EXISTS (
        SELECT * FROM users WHERE username = 'JonkiPro'
    );
    

    【讨论】:

    • 我有错误:关系“用户实体”不存在。在“UserEntity”应该是表的名称?
    猜你喜欢
    • 2015-03-15
    • 2015-08-06
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    相关资源
    最近更新 更多