Title: How to verify successful execution of queries using sqlcmd?
Tags: c#;sql-server;sqlcmd
Question: I am using the sqlcmd utility to run a sequence of sql queries in a script file. One of the simple queries is given below. The command I use to run the sqlcmd utility is:
sqlcmd.exe -m-1 -r 1 -S . -E -d test -i "D:\Recycle\Sp_tempTable1.sql".
I am running the sqlcmd utility from C# code. This works fine--the query is executed and the table is successfully created.
What I want is the utility to return something which tells me all of the queries in the input file ran successfully. Is there any means to do this?


Here is another answer: Using the -o flag allows you to specify an output file. This file will contain the number of affected rows and/or any errors encountered by the command. However, it would be MUCH easier to run the query from managed code using the System.Data.SqlClient namespace so you can implement standard try -> Exception -> catch error handling.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:08/18/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><sql-server><sqlcmd>', 'Title': 'How to verify successful execution of queries using sqlcmd?', 'ViewCount': 742, 'id': 12015900, 'user_AboutMe': '', 'user_AccountId': 1740961.0, 'user_DisplayName': 'ravi', 'user_Reputation': 123.0, 'user_Views': 38.0, 'user_id': 1592389.0}}