Python Forum

Full Version: export sql table to csv using BCP with headers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Team,

Can we export SQL Table into csv using BCP Approach.

bcp "SELECT (SELECT STUFF((SELECT '|'+name FROM sys.columns
WHERE object_id = OBJECT_ID('mydb.dbo.mytable') FOR XML PATH('')),1,1,''))
UNION ALL SELECT * FROM mydb.dbo.mytable"
out "myfile.csv" -c -t"|" -S myserver -U myusername -P mypassword -r \n

bcp "SELECT COLUMN_NAME FROM information_schema.columns WHERE TABLE_NAME = 'mytable' UNION ALL SELECT * FROM mydb.dbo.mytable" out "myfile.csv" -c -t, -S myserver -U myusername -P mypassword -r \n