복붙노트

[SQL] MS SQL 예외 : '@ P0'근처의 구문이 잘못되었습니다

SQL

MS SQL 예외 : '@ P0'근처의 구문이 잘못되었습니다

나는 MS SQL을 사용하여 DB를 조회하고있어 어떤 이유로 나는 다음과 같은 오류가 발생합니다 : com.microsoft.sqlserver.jdbc.SQLServerException을 : 근처의 구문이 잘못되었습니다 'P0 @'이 'P0는'내 구문 어디서나 아니더라도. ..

나는 누군가가 같은 문제가 있었다 그러나 그들은 내가 그의 솔루션은 나를 위해 작동하는 방법을 볼 수 없습니다, 그래서 내가 사용하지 않는 오전 저장된 프로 시저, 뭔가를 사용하고 읽었습니다. (그의 해결책은 프로 시저 호출 주위에 중괄호 {} 추가하는 방법에 대한 asomething 것.

어쨌든, 아래 I는 관련 코드를 붙여 넣은. 정말 누군가가 매우 좌절 점점이 좀 도와 수 있기를 바랍니다.

PreparedStatement stmt = null;
Connection conn = null;    

String sqlQuery = "SELECT TOP ? \n"+
                              "z.bankAccountNo, \n"+
                              "z.statementNo, \n"+
                              "z.transactionDate, \n"+
                              "z.description, \n"+
                              "z.amount, \n"+
                              "z.guid \n"+
                              "FROM \n"+
                              "( \n"+
                              "select  \n"+
                              "ROW_NUMBER() OVER (ORDER BY x.transactionDate, x.statementNo) AS RowNumber, \n"+
                              "x.transactionDate, \n"+
                              "x.statementNo, \n"+
                              "x.description, \n"+
                              "x.amount, \n"+
                              "x.bankAccountNo, \n"+
                              "x.guid \n"+
                              "FROM \n"+
                              "( \n"+
                              "SELECT  \n"+
                              "a.bankAccountNo,  \n"+
                              "a.statementNo,  \n"+
                              "a.transactionDate, \n"+
                              "a.description,  \n"+
                              "a.amount,  \n"+
                              "a.guid  \n"+
                              "FROM BankTransactions as a  \n"+
                              "LEFT OUTER JOIN BankTransactionCategories as b  \n"+
                              "ON a.category = b.categoryCode  \n"+
                              "WHERE b.categoryCode is null \n"+
                              ") as x \n"+
                              ") as z \n"+
                              "WHERE (z.RowNumber >= ?)";

stmt = conn.prepareStatement(sqlQuery);
stmt.setInt(1, RowCountToDisplay);
stmt.setInt(2, StartIndex);
ResultSet rs = null;
try{
    rs = stmt.executeQuery();
} catch (Exception Error){
    System.out.println("Error: "+Error);
}

사전에 감사합니다!

해결법

  1. ==============================

    1.SQL Server를 사용하면 변수에 전달하면 상단의 인수 괄호를 배치하도록 요구합니다 :

    SQL Server를 사용하면 변수에 전달하면 상단의 인수 괄호를 배치하도록 요구합니다 :

    SELECT TOP (?)
    
  2. ==============================

    2.우리의 응용 프로그램에서 우리는 depraceted SQLServerDialect을 드리고 있습니다. SQLServer2008Dialect로 변경 한 후 문제가 사라졌다.

    우리의 응용 프로그램에서 우리는 depraceted SQLServerDialect을 드리고 있습니다. SQLServer2008Dialect로 변경 한 후 문제가 사라졌다.

  3. ==============================

    3.5.x 버전으로 최대 절전 모드 업그레이드하고이 문제를 건너 왔어요. org.hibernate.dialect.SQLServerDialect에서 org.hibernate.dialect.SQLServer2012Dialect으로 업데이트 "hibernate.dialect"구성했다. 문제 해결!

    5.x 버전으로 최대 절전 모드 업그레이드하고이 문제를 건너 왔어요. org.hibernate.dialect.SQLServerDialect에서 org.hibernate.dialect.SQLServer2012Dialect으로 업데이트 "hibernate.dialect"구성했다. 문제 해결!

    문서 참조 최대 절전 모드 : https://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/session-configuration.html#configuration-programmatic

    락스 문제를 최대 절전 모드 : https://hibernate.atlassian.net/browse/HHH-10032

  4. ==============================

    4.같은 날 경우 그것은 또한 당신의 SQL 구문 오류가 발생할 수 있습니다이었다

    같은 날 경우 그것은 또한 당신의 SQL 구문 오류가 발생할 수 있습니다이었다

    select * from drivel d where exists (select * from drivel where d.id = drivel.id and drivel.start_date < '2015-02-05' AND '2015-02-05' < drivel.end_date) OR exists (select * from drivel where d.id = drivel.id and drivel.start_date < '2015-02-05' AND '2015-02-05' < drivel.end_date) OR exists (select * from drivel where d.id = drivel.id and '2015-02-05' < drivel.start_date and drivel.end_date < '2015-02-05'
    

    메시지를 준

    com.microsoft.sqlserver.jdbc.SQLServerException : 'P5 @'근처의 구문이 잘못되었습니다

    문제가 마지막에 빠진 ')'균형 사실이었다, 즉, 올바른 버전입니다

    select * from drivel d where exists (select * from drivel where d.id = drivel.id and drivel.start_date < '2015-02-05' AND '2015-02-05' < drivel.end_date) OR exists (select * from drivel where d.id = drivel.id and drivel.start_date < '2015-02-05' AND '2015-02-05' < drivel.end_date) OR exists (select * from drivel where d.id = drivel.id and '2015-02-05' < drivel.start_date and drivel.end_date < '2015-02-05')
    
  5. ==============================

    5.당신이 스프링 부팅 응용 프로그램 내에서 최대 절전 모드를 사용하는 경우 다음과 같은 구성 속성을 hibernate.dialect 설정할 수 있습니다 :

    당신이 스프링 부팅 응용 프로그램 내에서 최대 절전 모드를 사용하는 경우 다음과 같은 구성 속성을 hibernate.dialect 설정할 수 있습니다 :

    spring.jpa.database 플랫폼 = org.hibernate.dialect.SQLServer2008Dialect

  6. ==============================

    6.사용자 정의 데이터 소스를 사용하는 경우, 속성을 추가 :

    사용자 정의 데이터 소스를 사용하는 경우, 속성을 추가 :

    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.SQLServer2012Dialect
    

    application.properties에서 작동하지 않습니다.

    속성 데이터 소스 콩의지도대로 속성을 추가해야합니다 :

      @Bean
    public LocalContainerEntityManagerFactoryBean sqlServerEntityManagerFactory() {
        HashMap<String, String> properties = new HashMap<>();
        properties.put("hibernate.dialect", "org.hibernate.dialect.SQLServer2012Dialect");
    
        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setDataSource(sqlServerDataSource());
        factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        factoryBean.setJpaPropertyMap(properties);
        return factoryBean;
    }
    
  7. from https://stackoverflow.com/questions/7038818/ms-sql-exception-incorrect-syntax-near-p0 by cc-by-sa and MIT license