how to create temp table using jdbc java

Viewed 5399

I need to create a temp table in order to store some ids which i will process under a later query. I am receiving error

com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set.

When i execute my query for the creation of #temp table inside my sql. I don't need any resultset from this execution just need to create a temporary table with records. Please guide.

Code for my main query:


    String queryTempTable = "SELECT TOP (2) A.Id INTO #temp\n" +
                            "FROM  SALESDM.dbo.FactSales A\n" +
                            "INNER JOIN  SALESDM2.dbo.FactSales B\n" +
                            "ON A.Id = B.Id\n" +
                            "AND (\n" +
                            " A.sysDateModified = B.sysDateModified\n" +
                            " OR A.Id = B.Id\n" +
                            " OR A.ModifiedDatetime = B.ModifiedDatetime\n" +
                            " )";


                            System.out.println(queryTempTable);

                            if (conn == null) {
                                System.out.println("Unable to create Connection");
                            } else {
                                Statement stmtTempTable = conn.createStatement();
                                stmtTempTable.executeQuery(queryTempTable);
                            }


2 Answers

You should use executeQuery only when you are retrieving data and want a ResultSet.

If you are modifying data, then you should use execute:

stmtTempTable.execute(queryTempTable);

If possible create a view using the given query? This will act as a temporary table. And call the view later based on your requirement.

Related