Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

trying to export to .CSV with comma delimited AND double-quotes Expand / Collapse
Author
Message
Posted Thursday, May 20, 2010 1:49 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, September 26, 2010 10:23 PM
Points: 21, Visits: 44
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!
Post #924874
Posted Thursday, May 20, 2010 2:16 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, September 26, 2010 10:23 PM
Points: 21, Visits: 44
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!
Post #924890
Posted Wednesday, October 31, 2012 2:52 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, May 7, 2014 9:58 AM
Points: 24, Visits: 74
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
Post #1379538
Posted Wednesday, October 31, 2012 11:47 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:58 AM
Points: 36,995, Visits: 31,517
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1379631
Posted Wednesday, October 31, 2012 11:48 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:58 AM
Points: 36,995, Visits: 31,517
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1379632
Posted Thursday, November 1, 2012 7:36 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, May 7, 2014 9:58 AM
Points: 24, Visits: 74
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
Post #1379805
Posted Thursday, November 1, 2012 8:24 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, May 7, 2014 9:58 AM
Points: 24, Visits: 74
:-(
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 #1379842
Posted Thursday, November 1, 2012 9:51 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 11:23 AM
Points: 5,047, Visits: 11,799
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.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1379903
Posted Thursday, November 1, 2012 3:53 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:58 AM
Points: 36,995, Visits: 31,517
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1380069
Posted Thursday, November 1, 2012 4:46 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 3:08 PM
Points: 3,556, Visits: 7,671
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1380081
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse