cvillacr
asked on
stored procedure that runs a bcp with xp_cmdshell hangs
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.
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.
Wild guess....
@Path ends with a '\'
@Path ends with a '\'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
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.
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.
>>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
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
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.