Solved

Export MS SQL Procedure to text/csv file

Posted on 2010-09-17
7
1,038 Views
Last Modified: 2012-05-10
Hi,

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?  


0
Comment
Question by:darren-w-
7 Comments
 
LVL 10

Expert Comment

by:OnALearningCurve
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,

Mark.
0
 
LVL 24

Expert Comment

by:Tomas Helgi Johannsson
ID: 33699575
         Hi!

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


Regards,
   Tomas Helgi
0
 
LVL 24

Expert Comment

by:Tomas Helgi Johannsson
ID: 33699594
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 13

Author Comment

by:darren-w-
ID: 33699610
Hi,

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"

error:

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?
0
 
LVL 24

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. :)

Regards,
   Tomas Helgi
0
 
LVL 5

Expert Comment

by:ina_don
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?
0
 
LVL 13

Accepted Solution

by:
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.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now