Search code examples
javasql-serversqlcmd

Suppressing "X rows affected" in SQL Server using sqlcmd and Java


In my Java program, I am trying to execute a bunch of SQL scripts using sqlcmd via getRuntime.exec().

Earlier, I had been using osql this way -

osql -n -S SERVER -U sa -P PASSWORD -q "SET NOCOUNT ON" -i "INPUTSCRIPT.sql"

However, in the case of sqlcmd, the -i and -q switches are mutually exclusive. How do I do this in sqlcmd?

Note:

  1. I'd rather not modify the SQL scripts to include SET NOCOUNT ON in each file.

  2. There's already been a very similar question here. That solution discusses setting environment variables. Is that possible using Java?


Solution

  • You can specify multiple scripts for the i switch so you can do this:

    sqlcmd ... -i SetNoCountOn.sql,MyScript1.sql,MyScript2.sql
    

    That is, create a an extra script that just has SET NOCOUNT ON