0
我正在處理一個腳本以循環訪問Excel表,將參數傳遞給SQL Server過程,該過程需要三個參數並根據參數生成記錄。當我撥打電話時,它確實正確傳遞參數,執行程序並寫入記錄,但隨後出現運行時錯誤「1004查詢未運行...」Excel VBA調用SQL存儲過程返回虛假連接錯誤
我找到了意見狀態改變Background Query = False - 檢查。設置NoCount開 - 檢查...仍然出現錯誤。
SQL Server過程確實包含一個ReturnValue參數(1/0),我沒有通過,因爲它表示「太多參數」,當我嘗試這樣做時。這可能是問題嗎?不知道如何接收返回值。
Here IS my VBA代碼:
Sub CallProc()
Dim ws As Worksheet
Worksheets("AllTags").Activate
Dim Parm1 As String
Dim Parm2 As String
Dim Parm3 As Integer
Dim RowIndex As Long
Dim ReturnValue As Integer
RowIndex = 2
Do While Sheets("AllTags").Cells(RowIndex, 2).Value <> ""
If Sheets("AllTags").Cells(RowIndex, 3).Value > 0 Then
ReturnValue = 0
Parm1 = Sheets("AllTags").Cells(RowIndex, 1).Value
Parm2 = Sheets("AllTags").Cells(RowIndex, 2).Value
Parm3 = Sheets("AllTags").Cells(RowIndex, 3).Value
With ActiveWorkbook.Connections("TagUpload").OLEDBConnection
' SET NOCOUNT ON; ** IT DOESN'T LIKE THIS
.BackgroundQuery = False
.CommandText = "EXECUTE DCSTransfer.dbo.InsertTags '" & Parm1 & "', '" &
Parm2 & "', " & Parm3
.CommandType = xlCmdSql
.Connection = Array(_
"OLEDB;Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security
Info=True;Initial Catalog=DCSTransfer;Data Source=bdata03;Use Proc" _
, _
"edure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation
ID=B-MIS-NWILSON1;Use Encryption for Data=False;Tag with co" _
, "lumn collation when possible=False;")
.RefreshOnFileOpen = False
.SavePassword = False
.SourceConnectionFile = ""
.SourceDataFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
ActiveWorkbook.Connections("TagUpload").Refresh
.BackgroundQuery = False
On Error Resume Next ' **** NEVER GETS THIS FAR ***
End With
End If
RowIndex = RowIndex + 1
Loop
End
End Sub
Here IS該script時產生我manually call了procedure在SQL Server和ENTER該Parms:
USE [DCSTransfer]
GO
DECLARE @return_value int
EXEC @return_value = [dbo].[InsertTags]
@Badge = N'9046',
@StartTag = N'80996',
@Qty = 70
SELECT 'Return Value' = @return_value
GO
任何其他suggestions?
SET NOCOUNT上必須的代碼在存儲過程中的第一行,而不是在VBA代碼 – GuidoG
仍然得到同樣的錯誤。 –