Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I run exec dbo.subprogram from SQL Server Management Studio, it executes and returns result as many rows affected.

But when I run from cmd C:\Users\mssadmin>sqlcmd -S servername -d databasename -q "EXIT( exec dbo.subprogram)" it returns as 0 rows affected.

What could be the problem?

share|improve this question
Perhaps it doesn't affect any rows? Could it be possible that dbo.subprogram affects many rows when executed from SSMS and no rows when executed from sqlcmd? How about you post the actual code of this mysterious dbo.subprogram. – Remus Rusanu Oct 11 '12 at 8:37

If you really want to return the number of rows, You need to create a text file A.SQL:

exec dbo.subprogram; 
:EXIT(select @@rowcount)

Because EXIT must be at the beginning of a line, so you can't use -Q. Then you call:

C:\> SQLCMD -S servername -d database -i A.SQL -o A.OUT

Then the returned values by EXIT will be in ERRORLEVEL... You can query with:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.