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


trying to export to .CSV with comma delimited AND double-quotes


trying to export to .CSV with comma delimited AND double-quotes

Author
Message
hugh.hemington
hugh.hemington
SSC-Enthusiastic
SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)

Group: General Forum Members
Points: 141 Visits: 46
What I have is a simple table (in SQL 2005) that provides input to a mapping program.
It has four columns. I'm trying to automate the process of exporting it to .CSV because I'm tired of importing the table into Access, then exporting it to .CSV.
Here's what I need:
"Addr","City","State","Zip"
"123 Main St.","Glendale","CA","92203"
...
...

In other words, comma delimited, double-quoted data WITH a header row.
I found a post that used a created view, but since I'm trying to do this from inside a STORED PROCEDURE, I can't create a view. I haven't figured out or found how to do this with a format file. If I knew how to drive the output I want backwards through bcp and create a format file, it might produce something that would make the output correct the other direction.

If this is possible using BCP, is there a GOOD primer on it? (I've read all the bad ones already)
If not, does anyone have an idea how I can produce that file format?

Thanks!
hugh.hemington
hugh.hemington
SSC-Enthusiastic
SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)

Group: General Forum Members
Points: 141 Visits: 46
I figured out that I could "can" the views in advance, outside the stored procedure, but I'd still like to use BCP a lot better without so much hassle if possible.

So any good references on it, with lots of examples would be great!
cmcc
cmcc
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 75
Did you get any replies to this topic?
I, too, prefer to contain all the logic in a stored procedure rather than fuss with an SSIS package.

Colleen
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)

Group: General Forum Members
Points: 221477 Visits: 42003
This will do it.

bcp "SELECT QUOTENAME('Address',CHAR(34))+','+QUOTENAME('City',CHAR(34))+','+QUOTENAME('State',CHAR(34))+','+QUOTENAME('ZIP',CHAR(34)) d UNION ALL SELECT QUOTENAME(AddressLine1,CHAR(34))+',' + QUOTENAME(City,CHAR(34))+',' + QUOTENAME(StateProvinceID,CHAR(34))+',' + QUOTENAME(PostalCode,CHAR(34)) FROM AdventureWorks.Person.Address" queryout "C:\Temp\Contacts.txt" -c -T -SPutYourServer\InstanceNameHere


Change the "PutYourServer\InstanceNameHere" to the actual name of you Server and Instance name. This particular code runs against the AdventureWorks database. You'll need to change the FROM clause for that and the column names to match the table you're drawing from.

This code also writes to "C:\Temp\Contacts.txt" on the server. You can change that to whatever UNC you want.

Last but not least, this uses a Trusted Conection with Windows Authentication. If you only have SQL Server Authentication, then I don't recommend using this because you'll need to include the user name and password in clear text. Instead, you'd need to use %1 and %2 as batch substitution variables in a batch file and provide them at run time.

If your system is properly locked down for it, you could to this from a stored procedure using xp_CmdShell in a stored procedure or just from a job on the server.

--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
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)

Group: General Forum Members
Points: 221477 Visits: 42003
hugh.hemington (5/20/2010)
I figured out that I could "can" the views in advance, outside the stored procedure, but I'd still like to use BCP a lot better without so much hassle if possible.

So any good references on it, with lots of examples would be great!


Just one example is all that's needed... if it works. :-)

--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
cmcc
cmcc
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 75
Perfect! I can put this in an SP and just schedule that.

SSIS is giving me conniptions ; I'd like to stay in my comfortable T-SQL world where I am master of the universe as long as I can.

Off to try it out...

Colleen
cmcc
cmcc
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 75
:-(
Didn't work.
Getting this
...is too long. Maximum length is 128.
I use a view for the data, which is short enough.
It's getting the column names output that is causing me problems.

I'd use SSIS but I don't have time - I've been fiddling with this problem in SSIS for six weeks now, off and on, and keep tripping over one SSIS issue after another.
At least the BCP error makes sense to me.

In SSIS I have to change the file names - they have to be the date the package is run or the date from a parameter I have to pass in. This is a piece of cake from a parameter in a stored procedure but is taking a while to work through in SSIS.
i.e. I haven't gotten it to work yet.

ARGH!!!!
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)

Group: General Forum Members
Points: 53423 Visits: 21205
cmcc (11/1/2012)
:-(
Didn't work.
Getting this
...is too long. Maximum length is 128.
I use a view for the data, which is short enough.
It's getting the column names output that is causing me problems.

I'd use SSIS but I don't have time - I've been fiddling with this problem in SSIS for six weeks now, off and on, and keep tripping over one SSIS issue after another.
At least the BCP error makes sense to me.

In SSIS I have to change the file names - they have to be the date the package is run or the date from a parameter I have to pass in. This is a piece of cake from a parameter in a stored procedure but is taking a while to work through in SSIS.
i.e. I haven't gotten it to work yet.

ARGH!!!!


Haven't seen you posting these questions in the SSIS forum :-)

You could also, perhaps (if the columns are static), create a text file somewhere which contains only the column headings.

Then, after your BCP has run, use a simple DOS Copy to append one file to the other.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)

Group: General Forum Members
Points: 221477 Visits: 42003
cmcc (11/1/2012)
:-(
Didn't work.
Getting this
...is too long. Maximum length is 128.
I use a view for the data, which is short enough.
It's getting the column names output that is causing me problems.

I'd use SSIS but I don't have time - I've been fiddling with this problem in SSIS for six weeks now, off and on, and keep tripping over one SSIS issue after another.
At least the BCP error makes sense to me.

In SSIS I have to change the file names - they have to be the date the package is run or the date from a parameter I have to pass in. This is a piece of cake from a parameter in a stored procedure but is taking a while to work through in SSIS.
i.e. I haven't gotten it to work yet.

ARGH!!!!


Post your BCP command for this because, as you can see, my command is larger than 128. What may be wrong is a very large column name that QUOTENAME is tripping over. There's a fairly easy fix for that but it would help to see the actually BCP command you're using.

Also... do any of the columns in the BCP command have a datatype larger than VARCHAR(128) or NVARCHAR(64)???

--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
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)

Group: General Forum Members
Points: 43150 Visits: 19858
Out of curiosity, have you tried the import/export wizard?
It's a simple tool, just be sure to read all the options available.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
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