Link to home
Start Free TrialLog in
Avatar of Isaiah Melendez
Isaiah Melendez

asked on

export sql results to csv

what is the best way to do this via TSQL
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Use BCP Out
Avatar of Shaun Vermaak
Use BCP utility
bcp "SELECT Col1,Col2,Col3 FROM MyDatabase.dbo.MyTable" queryout "D:\MyTable.csv" -c -t , -S SERVERNAME -T

Open in new window

http://dba.stackexchange.com/questions/23566/writing-select-result-to-a-csv-file
--
declare @sql varchar(8000)  = ''
set @sql='exec master..xp_cmdshell ''bcp "select * from yourTable" queryout c:\temp\xxx.csv -c'''
exec(@sql)
--

Open in new window

@sj77: May I suggest you run BCP from CMD/Scheduled tasks. I would not use/enable xp_cmdshell
Using SQLShell.exe security tool

With xp_cmdshell disabled
User generated image
With xp_cmdshell enabled
User generated image
ASKER CERTIFIED SOLUTION
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Isaiah Melendez
Isaiah Melendez

ASKER

This ultimately helped us find the solution. Thank you!