Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 BCP queryout

Author  Topic 

josh2009
Yak Posting Veteran

81 Posts

Posted - 2009-11-13 : 10:20:44
I've been using the bcp utility to transfer tables in and out of out SQL database without any problems. I have been using the parameters -C -T succesfully when I use bcp. Now I need to be able to extract specific value from a column in table customers. However, when I use the queryout in bcp, I get an error. Here is my BCP statement -

bcp "select site from dbname.dbo.customers where ID = 1000" queryout c:\bcp\site.dat -C -T

Here is the error I get -

SQLState = 08001, NativeError = 2
Error = [Microsoft][SQL Native Client]Named Pipes Provider: Could not open a connection to SQL Server [2].
SQLState = 08001, NativeError = 2
Error = [Microsoft][SQL Native Client]An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections.
SQLState = S1T00, NativeError = 0
Error = [Microsoft][SQL Native Client]Login timeout expired

Any help will be greatly appreciated. Thanks in advance.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-13 : 10:43:54
You need to specify the -S switch so that it knows what SQL Server to use.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

josh2009
Yak Posting Veteran

81 Posts

Posted - 2009-11-13 : 11:08:58
That worked for me just fine. Thanks. The only thing is that when I tried outputting the value into a .txt file, the file contained random characters and not the site name that I was expecting. Is there a specific output file extension that I should be using? Thanks again.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-13 : 11:11:40
The file extension doesn't matter.

Here's what I would run:

bcp "select site from dbname.dbo.customers where ID = 1000" queryout c:\bcp\site.csv -c -T -t, -Sserver1\instance1 -r\r\n

I switched it to a csv extension purely because that's my standard for csv files. It really could be anything.


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-13 : 11:46:08
you sure you want -C and not -c

?????



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

josh2009
Yak Posting Veteran

81 Posts

Posted - 2009-11-13 : 12:11:10
.csv extension worked fine for me. I was hoping that it would be a txt file because I will be using the site value in my bcp command to output it to a folder named after the mobile site. The reason being right now, we have 10 different laptops which are used by mobile users. Typically, one mobile user goes to one site for a day. At the end of the day, all these mobile users would be merging their data using my merge solution. So my job is to merge the data between the different laptops and the production PC which I am successfully able to do with my sql stored procs. But to keep things straight, I need to be able to identify which records came from which site so that if I need to troubleshoot, I wont have to go through each and every text file output from my bcp command to get to the record I'm looking for.
Go to Top of Page

josh2009
Yak Posting Veteran

81 Posts

Posted - 2009-11-13 : 12:15:25
To add another layer of difficulty, we use the same virtual image on all the laptops so I cant use the computer name of the laptop for identification purposes. Not unless there's a another way for me to identify one laptop from a different one, I'd be more than happy to try that alternative. Thanks
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-13 : 15:14:04
Csv files are text files. The csv extension may default to Excel as the software program if you've got Office installed. It can still be opened in notepad or similar programs.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-13 : 15:22:00
let me get this straight

1. You are bcping data out of mobile laptops to a central file location
2. You are then bcping that data into a central sql server database
3. Then you combine all the data into 1 table

How does the file get to a central location

Is it on a network?

If it is, why can't you just use T-SQl and put the data into the central database to begin with?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

josh2009
Yak Posting Veteran

81 Posts

Posted - 2009-11-13 : 16:54:11
Because our mobile users at times leave the laptop in a secured warehouse that's why our solution was to provide USB drives for them to store the data from the laptop and then stick it in the production machine for merging. And I am bcpping multiple tables from one site into a stick and merging them along with other sticks from different sites into a central database. It would be a lot easier for me to trouble shoot if I can identify records from specific sites thus my question about the queryout parameter of bcp.

quote:
Originally posted by X002548

let me get this straight

1. You are bcping data out of mobile laptops to a central file location
2. You are then bcping that data into a central sql server database
3. Then you combine all the data into 1 table

How does the file get to a central location

Is it on a network?

If it is, why can't you just use T-SQl and put the data into the central database to begin with?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam





Go to Top of Page

josh2009
Yak Posting Veteran

81 Posts

Posted - 2009-11-13 : 16:58:32
Thanks Tara. I see. I think I may have found the solution. I am using the command - 'wmic bios get serial number' to get the serial number from the laptop. Each laptop has a designated site assigned so I can identify it that way. I then assign the serial number to a variable in my script and am able to create the folders I need to identify one laptop from another. I am also using the date stamp to further subdivide the records for easier identification. Thanks again for all your help. Very much appreciated.

quote:
Originally posted by tkizer

Csv files are text files. The csv extension may default to Excel as the software program if you've got Office installed. It can still be opened in notepad or similar programs.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-13 : 17:56:48
You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -