MS SQL Exception: Incorrect syntax near '@P0'

前端 未结 7 650
孤街浪徒
孤街浪徒 2020-12-03 16:36

I\'m querying a DB using MS SQL and for some reason I get the following error: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near \'@P0\' ev

相关标签:
7条回答
  • 2020-12-03 17:02

    Call the Procedure in the below way

    @Override
    public List<Rep_Holdings> getHoldingsReport(
            int pid
    )
    {
        List<Rep_Holdings> holdings = null;
    
        Session sess = sFac.getCurrentSession();
        if (sess != null && pid > 0)
        {
            @SuppressWarnings(
                "rawtypes"
            )
            Query query = sess.createSQLQuery(
                    "{CALL GetHoldingsforPF(:pid)}").addEntity(Rep_Holdings.class);
            query.setParameter("pid", pid);
    
            @SuppressWarnings(
                "rawtypes"
            )
            List result = query.getResultList();
            if (result != null)
            {
                if (result.size() > 0)
                {
                    holdings = new ArrayList<Rep_Holdings>();
                    for (int i = 0; i < result.size(); i++)
                    {
                        Rep_Holdings holding = (Rep_Holdings) result.get(i);
                        holdings.add(holding);
                    }
                }
            }
        }
    
        return holdings;
    }
    

    The same procedure in SQL Server

    ALTER PROCEDURE [dbo].[GetHoldingsforPF]
        @pid int
    AS
    BEGIN
    SET NOCOUNT ON;
            -- Insert statements for procedure here
    
            declare @totalPFInv decimal(15,2);
    
        set @totalPFInv =  ( select  sum(totalInvestment) from Holdings where pid = @pid );
    
        Select hid,
               pid,
               scCode,
               numUnits,
               avgPPU,
               adjPPU,
               totalInvestment,
               cast ( (totalInvestment/@totalPFInv)*100 as decimal(10,1)) as perPF,
               totalDiv,
               cast ( (totalDiv/totalInvestment)*100 as decimal(10,1)) as divY
               from Holdings
               where pid = @pid
    END
    
    0 讨论(0)
  • 2020-12-03 17:03

    Upgraded hibernate to version 5.x and came across this issue. Had to update "hibernate.dialect" configuration from org.hibernate.dialect.SQLServerDialect to org.hibernate.dialect.SQLServer2012Dialect. Fixed the issue!

    Hibernate Doc Reference: https://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/session-configuration.html#configuration-programmatic

    Hibernate Jira issue: https://hibernate.atlassian.net/browse/HHH-10032

    0 讨论(0)
  • 2020-12-03 17:05

    In our application we have extended a depraceted SQLServerDialect. After change to SQLServer2008Dialect the problem disappeared.

    0 讨论(0)
  • 2020-12-03 17:09

    SQL Server requires you to place parenthesis around the argument to top if you pass in a variable:

    SELECT TOP (?)
    
    0 讨论(0)
  • 2020-12-03 17:18

    If you are using custom data source, adding property :

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

    in application.properties will not work.

    You have to add the property as properties map in your data source bean:

      @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;
    }
    
    0 讨论(0)
  • 2020-12-03 17:24

    If you are using Hibernate within a spring-boot app, you may set hibernate.dialect with following configuration property:

    spring.jpa.database-platform=org.hibernate.dialect.SQLServer2008Dialect

    0 讨论(0)
提交回复
热议问题