我有一个使用 bcp 创建在 cmdshell 中执行的 csv 文件的存储过程。当我在 sql server 2012 中运行存储过程时,它会像我需要的那样执行。但是当我从 excel VBA 运行它时,它只会捕获循环的第一次执行然后停止。没有错误,但我可以说它没有完全运行。
我在下面发布了 Vba 和 Sql 存储过程代码。我很难在网上找到任何有用的东西,所以我想我会问堆栈溢出!任何帮助将非常感激。谢谢!
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