Solved

stored procedure that runs a bcp with xp_cmdshell hangs

Posted on 2004-04-29
6
2,139 Views
Last Modified: 2007-12-19
Hi experts,

I have a procedure that writes a file
(@path defines where bcp.exe is and the directory where the output file will be located.
......

set @sql = @path + '\bcp "SELECT ltrim(Data1) FROM ' + @db_name + '..CTS_SendFile (nolock) " queryout "' + @path + '\CTSABO.TXT" -c -CACP -S' + @servername + ' -U' + @user + ' -P' + @pwd
exec @resp2 = master..xp_cmdshell @sql, no_output

......

it's hanging but It works when I don't consider the path for bcp.exe:

set @sql = 'bcp "SELECT ltrim(Data1) FROM ' + @db_name + '..CTS_SendFile (nolock) " queryout "' + @path + '\CTSABO.TXT" -c -CACP -S' + @servername + ' -U' + @user + ' -P' + @pwd
exec @resp2 = master..xp_cmdshell @sql, no_output

I'm considering to trust that bcp.exe is defined in PATH, but I'd like to know what's causing the problem.

Thanks
Carlos V.
0
Comment
Question by:cvillacr
6 Comments
 
LVL 6

Expert Comment

by:robertjbarker
ID: 10956441
To debug this I would put a print statement right after the "set @sql" statement:

print @sql

I would run my code in Query Analyzer to just past the  print statement.  Then I would cut the result from the Message panel and paste it into a command box to see what happens.
0
 
LVL 11

Expert Comment

by:rdrunner
ID: 10956976
Wild guess....

@Path ends with a '\'
0
 
LVL 26

Accepted Solution

by:
Hilaire earned 250 total points
ID: 10957437
Did you make a copy of the bcp utility to another @path folder ?

BCP.exe is intended to run in the
MS SQL Server/version/tools/binn folder
It's expecting some required dlls to be present in the same directory

When you run bcp from any location, it uses the bcp.exe that is in hte %PATH% env variable.

If you make a copy in c:\temp and run it from there, it will fail saying missing dll

HTH

Hilaire
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 26

Expert Comment

by:Hilaire
ID: 10957507
When you run the bcp utility from within the xp_cmdshell system proc, it runs on the server.
You can definitely assume that the server has a correct path.

You can also use a fully qualified path to the bcp utility ON THE SERVER, this should make no harm.
Again, this path is most likely different from the path you used to test the bcp command on your local machine.
You must use the bcp utility in the default folder, not a copy in a another folder (this makes the command hang)

Hilaire
0
 

Author Comment

by:cvillacr
ID: 10967877
Hi Hilaire, robertjbarker

The SP was tested, all the variables are well assigned. You're right Hilaire, I copied the bcp.exe to specific directory. The problem is that the software is working ok in the production environment, when I copied everything to my PC I have this problem. I think the solution will be to trust in the path of sql server. But any cause the SP is working ok in production with the bcp.exe copied in a specific path?

I tried copying all .dll from \bin  to the specific directory and bcp.exe is hanging again...

Thanks
Carlos V.
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 10975427
>>I think the solution will be to trust in the path of sql server<<
Definitely

>>But any cause the SP is working ok in production with the bcp.exe copied in a specific path?<<
I guess you have more services loaded on the production server than on your PC. (SQL server agent for DTS jobs and backups, ...)
Chances are that when bcp is called on the server, the required dlls are already loaded in memory so SQL Server doesn't have to search them in the local directory.

BTW I think it's better to stick to default paths defined on the server, thus you have nothing to hard-code for the paths, and no dlls to copy if you make a server upgrade

Cheers

Hilaire
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

706 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

18 Experts available now in Live!

Get 1:1 Help Now