views:

439

answers:

1

Hello all,

I have this BCP command:

'bcp DBName..vieter out c:\test003.txt -c -T /t"\",\"" -S SERVER'

The output CSV I get does not put quotes around the field names, instead it puts it around the commas! How can I get the /t"\",\"" to put quotes around all fields.

Thanks all

+1  A: 

Setting the row terminator in addition to the field terminator should do the trick

'bcp DBName..vieter out c:\test003.txt -c -T -t"\",\"" -r"\"\n\"" -S SERVER'

This will likely work, but miss of the leading " for the first field of the first line, and perhaps the last of the last, i'm not sure, just guessing really, no server here!

or try using QUOTENAME to wrap text fields (you could also wrap numbers, but that isn't normally required.

'bcp "SELECT id, age, QUOTENAME(name,'"') FROM DBName..vieter" queryout c:\test003.txt -c -T -t"," -S SERVER'
Paul Creasey
Is there ever a way to have the first and last have quotes - I really need a well formatted CSV file.
Abs
What you said happened - but the last fiedl had two quotes at the end and the first had no quotes. Do I just need to fiddle with the above to get what I want? Is there some docs I can look at as I haven't come across this regex stuff - if it is regex!
Abs
you could use `QUERYOUT` and then create a query using `QUOTENAME(column,'"')` for text fields.
Paul Creasey
Can I use QUOTENAME to quote all columns without passing the name of the column as I need to do this for different tables? I tried this but I got quite a few errors - is the quoting correct? I am using the above in `EXEC master..xp_cmdshell @bcpCommand`
Abs
You could potentially set @bcpCommand programmatically by querying syscolums and building the query, but it's getting very messy, not something i would want to do. This seems like a major problem with BCP!
Paul Creasey
Thanks Paul - I am not sure if I can go any further with this. Your answer is so close, but I can't believe I can't add a quote to the first field and remove one from the very last field of the last line! Damn it! Is it even possible?
Abs