SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 bcp command hangs stored procedure
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Supriya Krishna
Starting Member

1 Posts

Posted - 09/30/2013 :  17:29:46  Show Profile  Reply with Quote
I have a problem where bcp command is called and executed successfully sometimes but fails randomly.

My stored procedure looks like this:

select @SQL = 'select * from [' + @RepositoryName + '] where ( IsNumeric([' + @PhoneField + ']) = ' + CONVERT(varchar(1), @FALSE) + ' OR LEN([' + @PhoneField + ']) <> ' + CONVERT(varchar(2), @ValidLength) + ')'

execute(@SQL)

set @Count = @@ROWCOUNT

if @Count > 0


EXEC sp_configure 'show advanced options', 1
RECONFIGURE
EXEC sp_configure 'xp_cmdshell', 1
RECONFIGURE

set @CommandSQL= 'bcp "SELECT * FROM ['+DB_NAME()+'].[dbo].[' + @RepositoryName + '] where ( IsNumeric([' + @PhoneField + ']) = ' + CONVERT(varchar(1), @FALSE) + ' OR LEN([' + @PhoneField + ']) <> ' + CONVERT(varchar(2), @ValidLength) + ')' " queryout "C:\Export\' + @ExportFileName + '.csv" -c -t, -T -S ' + @@SERVERNAME

BEGIN TRAN
EXEC master..xp_cmdshell @CommandSQL

IF @@ERROR <> 0 and @@TRANCOUNT <> 0
ROLLBACK TRAN

ELSE IF @@ERROR = 0 and @@TRANCOUNT > 0
COMMIT TRAN

set @SQL='delete from [' + @RepositoryName + '] with (Rowlock) where ( IsNumeric([' + @PhoneField + ']) = ' + CONVERT(varchar(1), @FALSE) + ' OR LEN([' + @PhoneField + ']) <> ' + CONVERT(varchar(2), @ValidLength) + ')'

execute(@SQL)


The above procedure is called 4 times from another procedure by passing appropriate parameters. Most of the time, it executes successfully for 2 times before failing. When I try to debug, it always hangs at EXEC master..xp_cmdshell @CommandSQL. The task manager shows bcp.exe as running at this point. Interestingly, the required rows are exported using the bcp command, but the stored procedure does not move further until bcp.exe is manually stopped from the task manager.

Any help is appreciated, thanks!

tkizer
Almighty SQL Goddess

USA
37142 Posts

Posted - 09/30/2013 :  19:14:51  Show Profile  Visit tkizer's Homepage  Reply with Quote
You need to wait until it finishes. It will move to the next command in the stored procedure when bcp.exe is done exporting the data.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Maa421S
Starting Member

3 Posts

Posted - 06/09/2014 :  11:10:14  Show Profile  Reply with Quote
OP did you ever figure this out? I am having the same issue. Running BCP from SP and it randomly fails and hangs.

Thanks!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000