![](/img/trans.png)
[英]Spring boot data.sql does not initialize data in Postgresql
[英]Wrong SQL statements syntax in file data.sql in Spring Boot nad PostgreSQL
我想創建一個將由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
中的另一個表中
有關有條件創建用戶的另一種方法,請參見此問題 ,因為您似乎必須先加載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'
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.