1

I have Java code to bulk-insert tab file to SQL Server. I want to get the count of how many records were inserted. I tried using @@rowcount but I'm getting an error that "Statement did not return a result set". If I run the bulk insert statement in management studio, I can get the count.

Statement stmt = sqlConnection.createStatement();         
ResultSet rs = stmt.executeQuery ("BULK INSERT schema1.table1 FROM 'd:\temp1\file1.tab' SELECT @@rowcount");

Is there any way to get the inserted count?

rgettman
  • 176,041
  • 30
  • 275
  • 357
Paresh
  • 564
  • 6
  • 23
  • Perhaps something like the following? https://stackoverflow.com/a/10139221/6492765 – MK_ Apr 04 '18 at 20:37
  • what is happening when you use execute(query) statement instead of executequery(query) – Eray Balkanli Apr 04 '18 at 20:37
  • This could help! http://www.java2s.com/Code/Java/Database-SQL-JDBC/RetrievearowcountfromaResultSet.htm – Fleury26 Apr 04 '18 at 20:39
  • @ErayBalkanli execute returns false so cant get count. Those examples are of select statements which works fine. Mine is "Bulk Insert" which gives the error. – Paresh Apr 04 '18 at 20:47

1 Answers1

3

I'm not familiar with SQL Server but it seems like you'll want to issue an executeUpdate instead of an executeQuery

Statement stmt = sqlConnection.createStatement();         
int insertedRowCount = stmt.executeUpdate("BULK INSERT schema1.table1 FROM 'd:\temp1\file1.tab'");
Richard Tran
  • 438
  • 3
  • 10
  • This gives me row count. But in my bulk insert statement, I have batch size=1000 so this always return 1000 but my file has lets say 5000 entries. – Paresh Apr 05 '18 at 13:58