1

How to save query result using T-SQL in a csv file in SSMS 2008? Needs to be done programmatically.

3
  • I specifically said using T-SQL. Programmatically. Which means not through the GUI. Commented Sep 4, 2009 at 0:24
  • "In SSMS" MS suggests Management Studio to me. Commented Sep 4, 2009 at 7:08
  • The query window in SSMS Commented Sep 4, 2009 at 19:31

2 Answers 2

1

You could use System.Diagnostics.Process to run the SQLCMD or OSQL or BCP programs to dump out csv files.

Sign up to request clarification or add additional context in comments.

2 Comments

Agree BCP is simplest way; this is a good primer simple-talk.com/sql/database-administration/… If BCP isnt an option then a CLR function would let you do it too.
osql and bcp can't be run from the query window as these are command line programs. sqlcmd could work if you have access to it.
0

Here is an example I found from google. Play around with the parameters.

EXEC master..xp_cmdshell 'sqlcmd -X -S server -q "SET NOCOUNT ON Select 'department','FundCode','Month' SELECT * FROM pubs.dbo.test" -s , -w 255 -u -o "\\server\output.csv" -h-1'

Comments

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.