Export MS SQL Procedure to text/csv file

Posted on 2010-09-17
Last Modified: 2012-05-10

I have a MS SQL procedure that excepts a single numerical parameter which  then produces a single table of results.

I would like to export these result sets to a file so that it can be imported into an existing mysql table.

I am trying to use bcp but it only seems to produce one line and fail, this is the syntax I am using:

bcp "exec cwo.djw.ol_lcd_files 11" queryout "F:\EXPORT_LCD_OFFLINE\venues.csv" -t","  -e "C:\error.log" -c -T -SSERVER -r \r\n

the query should produce about 7000 lines but all I get is one single line like this:
2689,7068,Training 001, , , , , ,,,,, ,,,

Is anyone able to tell me where I am going wrong?  

Question by:darren-w-
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 10

Expert Comment

ID: 33699539
Hi darren-w-,

have you tried encapsulating your row termination options in quotes which would give

bcp "exec cwo.djw.ol_lcd_files 11" queryout "F:\EXPORT_LCD_OFFLINE\venues.csv" -t","  -e "C:\error.log" -c -T -SSERVER -r "\r\n"

Just a shot in the dark,

LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 33699575

It seems like your function is returning a pointer/cursor to a result-table which you need to
iterate through.

   Tomas Helgi
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 33699594
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

LVL 13

Author Comment

ID: 33699610

this is what I'm getting from this query
bcp "exec cwo.djw.ol_lcd_files 11" queryout "F:\EXPORT\venues.csv"  -c  -SSERVER   -U user -P password -r "\r\n" -t"," -e "C:\WINDOWS\Temp\error.txt"


Starting copy...
SQLState = S1000, NativeError = 0
Error = [Microsoft][SQL Native Client]Protocol error in TDS stream
SQLState = S1000, NativeError = 0
Error = [Microsoft][SQL Native Client]Protocol error in TDS stream
SQLState = S1000, NativeError = 0
Error = [Microsoft][SQL Native Client]Protocol error in TDS stream

BCP copy out failed

I've got the feeling that it does not like the extracted data, though I would assume it should be able to handle all output?
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 33699621
Forgot to mention this

From the msdn link I posted

"Is a Transact-SQL query that returns a result set. If the query returns multiple result sets, such as a SELECT statement that specifies a COMPUTE clause, only the first result set is copied to the data file; subsequent result sets are ignored. Use double quotation marks around the query and single quotation marks around anything embedded in the query. queryout must also be specified when bulk copying data from a query."

Try changing queryout to just out. :)

   Tomas Helgi

Expert Comment

ID: 33709094
I think your terminator could be causing the problems that you are experiencing. Instead of using -t "," could you try letting it use the default terminator or could you atleast use something else to see if the export is successful then we can work on this from there?
LVL 13

Accepted Solution

darren-w- earned 0 total points
ID: 33718296
I've tried a few of these options and not had much success, now given up on this method and I've adapted a php program to do the job.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Duplicated data in GROUP_CONCAT 2 24
removing unwanted rows from an sql server ranked table 13 36
SQL Syntax 6 32
SQL query 7 20
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question