Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create temp table using jdbc java

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);
                            }


like image 804
Amad Bin Mumtaz Avatar asked Sep 20 '25 06:09

Amad Bin Mumtaz


1 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);
like image 90
Brice Frisco Avatar answered Sep 23 '25 07:09

Brice Frisco