Python Forum
export sql table to csv using BCP with headers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
export sql table to csv using BCP with headers
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to store columns of a .csv in variables (when the .csv has no headers) ? hobbyist 6 1,274 Aug-18-2023, 02:06 PM
Last Post: deanhystad
  TreeView column headers TWB 2 2,207 Jan-29-2023, 02:13 PM
Last Post: TWB
  Code changing rder of headers Led_Zeppelin 0 914 Jul-13-2022, 05:38 PM
Last Post: Led_Zeppelin
  Request Headers (scheme) JohnnyCoffee 0 1,921 Mar-31-2021, 09:17 PM
Last Post: JohnnyCoffee
  Reading csv with multiple "headers" Clives 3 2,533 Dec-31-2020, 09:25 AM
Last Post: Ana_junior
  Fetch Oracle DB rows & print it in HTML file with table's col headers in table format tssr_2001 1 3,006 Sep-04-2020, 01:39 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020