过去,我在运行单线程SP方面取得了成功,并使用以下命令等待“ 0”或“ -1”答复以指示成功:
Set cnn = New ADODB.Connection
cnn.CursorLocation = adUseClient
cnn.ConnectionString = "driver={SQL Server};server=" & TempVars!my_ip & ";Trusted_Connection=no;Database=" & TempVars!my_Database & ";UID=username;PWD=password"
cnn.CommandTimeout = 0
cnn.Open
strSQL = "ExportToCSVZip 'myTable', '\\192.168.242.147\InventoryProcessing\Exports\', 'ProdCIVProcess', 'Y';"
Set rs = cnn.Execute(strSQL)
'SP Result -1 = Success, 0 = Fail
If rs.Fields(0) = -1 Then
msgbox "Done"
Else
msgbox "Fail"
End If
我可以使用以下方式异步运行它:
cnn.Execute strSQL, adExecuteNoRecords, adAsyncExecute
但是我不清楚这种形式的正确语法来确定Return标志。我的尝试返回了“常规”错误。
建议?
答案 0 :(得分:1)
根据您的确切配置,您可以轻松返回参数。
假定您具有以下SP:
CREATE PROCEDURE TestSP
@Param1 INT OUT
AS
WAITFOR DELAY '00:00:03';
SET @Param1 = 5;
然后,在您的模块中全局声明:
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command
然后,在您的函数中:
Set cnn = New ADODB.Connection
cnn.Open = "Some connectionstring"
Set cmd = New ADODB.Command
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "TestSP"
Dim p As ADODB.Parameter
Set p = cmd.CreateParameter("@Param1", adInteger, adParamOutput, 8)
cmd.Parameters.Append p
Set cmd.ActiveConnection = conn
cmd.Execute Options:=adAsyncExecute
然后,在一个单独的函数中,测试命令是否完成,如果是,则返回值
If cmd.State <> adStateExecuting Then
returnValue = cmd.Parameters("@Param1").Value 'Returns 5 when done
End If
'Because of the global scope, cleanup is required
conn.Close
Set conn = Nothing
Set cmd = Nothing