0

I need to write a batch file that will, among other things, connect to a MS SQL server and extract all the content of 3 tables (dbo.Group, dbo.Membership and dbo.Person) to 3 separate CSV files. The CSV files should be with headers (column names in the tables), semicolon separated and UTF-8.

The machine running the batch file will be Windows 10 or Server 2012, connecting to another server on the network running MS SQL Server.

Any ideas how to go about this?

2
  • Are you sure this needs to be fully contained in a batch file? You do have a SQL server... Commented Nov 22, 2017 at 13:32
  • Yes, unfortunately. It is all part of A Bigger Thing. Commented Nov 22, 2017 at 15:37

1 Answer 1

1

Use sqlcmd Utility, for instance one command:

SQLCMD -S SERVER/INSTANCE -E -Q "SELECT * FROM Table1" -s ";" -o "C:\temp\file1.csv" -W
Sign up to request clarification or add additional context in comments.

3 Comments

That did it! I ran the script as you wrote, added -u to get unicode and it works very well. Only question is that "(103 rows affected)" was added to the end of the CSV file. Is there any way to avoid this?
I think, adding "set nocount on" to the beginning of your query will solve this problem
this works well but for some reason the output has a second row with ----- , -----,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.