I have a stored procedure that uses bcp to create csv files executed in the cmdshell. When I run the stored procedure in sql server 2012 it executes like I need it to. But when I run it from excel VBA it only catches the first execution of the loop then stops. There is no error, but I can tell it didn't run completely.
I posted the Vba and Sql stored procedure code below. I am struggling to find anything helpful online so I figured I'd ask stack overflow! Any help would be much appreciated. Thank you!
Dim i As Long, folder As String, year1 As String, month1 As String, day1 As String, data As String, hour1 As String, minute1 As String
Dim cn As New ADODB.Connection
'Open the connection
strCon23 = "Driver={SQL Server};Server=*******"
cn.CommandTimeout = 0
cn.Open strCon23
s = "EXEC SendNewRates"
cn.Execute s
cn.Close
DECLARE @Date2 datetime
SET @Date2=getdate()
Declare @sql varchar(1250)
DECLARE @hour varchar(25), @min varchar(25), @sec varchar(25)
Declare @start VARCHAR(25), @Interval bigint, @num varchar(200), @max bigint,@end varchar(200)
Set @Hour=datepart(hh,@Date2)
Set @Min=datepart(Mi,@Date2)
Set @Sec=datepart(ss,@Date2)
Set @start=1
Set @Interval=50000
Set @End=@Interval
Set @num=1
Set @max = (SELECT max(ROWNUM) FROM UPLOADTable )
While @Start<@Max
Begin
set @SQL='bcp "Select Data From ******* Where [RowNum]>='+@start+' and [RowNum]<='+@end +' order by [RowNum]" '+'queryout C:\Test\Upload_NextGen_'+@Hour+'_'+@Min+'_'+@sec+'_'+@num+'_.csv -c -T -t -S PRK- PR\EMULATION'
EXEC master..XP_CMDSHELL @sql
Set @num=@num+1
Set @Start=Convert(bigint,@Start)+convert(bigint,@Interval)
Set @End=Convert(bigint,@end)+convert(bigint,@Interval)
End