How to display number of rows affected in a text file sql

Viewed 458

I am using a batch file to run SQL statements from another file. How do I log the result or error message on a text file in same folder. Here is what I have done:"

@echo off
sqlcmd.exe -S "(localdb)\v11.0" -i C:\SQLBatch\CreateLinkServer.sql -o C:\SQLBatchCreateLinkServer.txt

The log file (SQLBatchCreateLinkServer.txt) only contains "Changed database context to 'master'.". How do I get the batch to write all the details in the log such as no. of rows affected and which query is being excuted.

Thank you

1 Answers

The suggestion from Parfait helped resolve the issue.

I had this

@echo off sqlcmd.exe -S "(localdb)\v11.0" -i C:\SQLBatch\CreateLinkServer.sql -o C:\SQLBatchCreateLinkServer.txt

instead of

@echo off sqlcmd.exe -S "(localdb)\v11.0" -i C:\SQLBatch\CreateLinkServer.sql -o C:\SQLBatch\CreateLinkServer.txt

hence the count not writing to specified file.

The difference is that instead of the name of the file being SQLBatchCreateLinkServer.txt, it should have been CreateLinkServer.txt in a folder named SQLBatch

Related