简体   繁体   中英

Hibernate call sql server stored procedure throws Incorrect syntax near '@P0'

the sql server stored procedure is as follows:

CREATE PROCEDURE SelectStaff @rankabbr varchar(14), @sex varchar(1)
AS
SELECT * FROM staff WHERE rankabbr = @rankabbr AND sex = @sex

and I can successfully run it in SSMS by

exec SelectStaff  'AEO', 'M';

but in eclipse, I use hibernate to call this stored procedure as follow:

Session session2 = HibernateUtil.getCurrentSession();
org.hibernate.Query query = session2.createSQLQuery(" call SelectStaff(:rankabbr, :sex) ")                    
          .setParameter("rankabbr","AEO")
          .setParameter("sex", "M");
List<Object[]> results =query.list();

but error return as follow:

23:30:57,708 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-7) SQL Error: 102, SQLState: S0001 23:30:57,741 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-7) Incorrect syntax near '@P0'.

what is the reason? I have tried to add { } in the call as

org.hibernate.Query query = session2.createSQLQuery("{ call SelectStaff(:rankabbr, :sex) }")                      
                      .setParameter("rankabbr","AEO")
                      .setParameter("sex", "M");

but the error is the same.

I try to replace SQLServerDialect with SQLServer2012Dialect but the same error.

I find the following code is work:

Session session2 = HibernateUtil.getCurrentSession();
ProcedureCall call = session2.createStoredProcedureCall("SelectStaff");
call.registerParameter( 1, String.class, ParameterMode.IN).bindValue("AEO");
call.registerParameter( 2, String.class, ParameterMode.IN).bindValue("M");
Output output = call.getOutputs().getCurrent();
List<Object[]> results;
if (output.isResultSet()) {
     results = ( (ResultSetOutput) output ).getResultList();
     for (Object[] row:results) {
         // ......
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM