SQL Error - Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '@P0'

We have a simple query that was working with Hibernate 4 and is now not working after updating to Hibernate 5.6.12. It creates a temporary table to store temporary data in order to process the request, and it gets deleted once the request is handled.

private void dropTempTable(Session session, String tableName) {
    Query query=session.createSQLQuery("drop table ?");
    query=query.setParameter(1,tableName);
    query.executeUpdate();
}

private void createTempTable(Session session, String tableName) {
    Query query=session.createSQLQuery("create table ?" + " (" +
            "ID numeric(19,0) not null, " +
            "COM_ID numeric(19,0) not null, " +
            "SESSIONID varchar(256) not null, " +
            "TIME numeric(19,0) not null)");
    query=query.setParameter(1,tableName);
    query.executeUpdate();
}

And following exception caught,

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '@P0'.
       at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:217) 
       at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1655) 
       at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:440) 
       at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:385) 
       at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7505) 
       at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2445) 
       at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:191) 
       at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:166) 
       at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:328) 
       at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102) 
       at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204) 
       at org.hibernate.engine.query.spi.NativeSQLQueryPlan.performExecuteUpdate(NativeSQLQueryPlan.java:194) 
       at org.hibernate.internal.SessionImpl.executeNativeUpdate(SessionImpl.java:1528) 
       at org.hibernate.query.internal.NativeQueryImpl.doExecuteUpdate(NativeQueryImpl.java:264) 
       at org.hibernate.query.internal.AbstractProducedQuery.executeUpdate(AbstractProducedQuery.java:1500) 
       at com.stream.dao.EventTableImp.dropTemporaryTable(EventTableImp.java:155) 

I read that someone encountered the same issue and was able to resolve it by changing the dialect, SQLServer2008Dialect to SQLServer2012Dialect. But this didn’t work for me. Can anyone help me out?

You can’t create a prepared statement for DDL like a create table statement. You have to concatenate the name to the SQL string instead of using ? (parameter marker)