SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Export sql query to CSV file


Export sql query to CSV file

Author
Message
ilugopal
ilugopal
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 8
While I'm exporting sql query details to CSV file I get the column headers and after the column I get minus symbol below the column headers followed by the data. IS there a way where I can get only the column headers and not the minus symbol.



Server_Name Instance_name User_name
------------- -------------- ----------
xys.com sql24 user1
xys.com sql24 user1
xys.com sql24 user1

I dont want this "---------" symbol. I want the o/p as

Server_Name Instance_name User_name
xys.com sql24 user1
xys.com sql24 user1
xys.com sql24 user1
Stuart Davies
Stuart Davies
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

Group: General Forum Members
Points: 7379 Visits: 4817
Two options in my opinion:-

1 Run the query to grid then on the grid Right click on grid -> Select all -> Right click on grid ->copy with headers -> paste into excel save as CSV.

2 If you want to run this on a scheduled basis use SSIS to export the data as csv using a query along the lines of select Server_Name, Instance_name, User_name union all select col1, col2, col3 from table"

-------------------------------Posting Data Etiquette - Jeff Moden Smart way to ask a questionThere are naive questions, tedious questions, ill-phrased questions, questions put after inadequate self-criticism. But every question is a cry to understand (the world). There is no such thing as a dumb question. ― Carl Sagan I would never join a club that would allow me as a member - Groucho Marx
Robert klimes
Robert klimes
SSCarpal Tunnel
SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)

Group: General Forum Members
Points: 4993 Visits: 3464
you could use BCP as well.

BCP "select col1+','+col2+','+col3 from dbo.table" queryout c:\temp\file.csv -c -T


or if you have xp_cmdshell enabled you can run it from SSMS or sql agent(note: xp_cmdshell can be a security risk).

exec xp_cmdshell ''bcp "select col1+','+col2+','+col3 from dbo.table" queryout c:\temp\file.csv -c -T''



Bob
-----------------------------------------------------------------------------
How to post to get the best help
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (213K reputation)SSC Guru (213K reputation)SSC Guru (213K reputation)SSC Guru (213K reputation)SSC Guru (213K reputation)SSC Guru (213K reputation)SSC Guru (213K reputation)SSC Guru (213K reputation)

Group: General Forum Members
Points: 213101 Visits: 41977
ilugopal (2/22/2013)
While I'm exporting sql query details to CSV file I get the column headers and after the column I get minus symbol below the column headers followed by the data. IS there a way where I can get only the column headers and not the minus symbol.



Server_Name Instance_name User_name
------------- -------------- ----------
xys.com sql24 user1
xys.com sql24 user1
xys.com sql24 user1

I dont want this "---------" symbol. I want the o/p as

Server_Name Instance_name User_name
xys.com sql24 user1
xys.com sql24 user1
xys.com sql24 user1


How are you doing the export? For example, are you using SQLCmd, BCP, or someother method? I ask because it does make a real difference.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search