2017-04-11 114 views
1

我試圖將表返回到具有50 +列的MS Access,並且行可以從0到5000變化。對於每種情況,可以有多個記錄和車輛類型。SQL Server通過存儲過程將表返回到MS Access

我可以執行存儲過程,它工作正常。我無法將數據返回到MS Access。

存儲的過程代碼:

ALTER PROCEDURE [dbo].[pJDB_Export] 
    (@dteFrom int, 
    @dteTo int, 
    @Veh nvarchar(80)) 
AS 
BEGIN 
    SET NOCOUNT ON; 

    DECLARE @stWhere VARCHAR(200) 

    SELECT DISTINCT [Case] 
    INTO #tmp 
    FROM data 
    WHERE [VEHICLE TYPE] = @Veh 
     AND (CY BETWEEN @dteFrom AND @dteTo) 

    SELECT DISTINCT * 
    FROM dbo.vdata_Export_V3_3_2 v 
    INNER JOIN #tmp t ON v.[CASE] = t.[CASE] 

MS接入代碼:

Function Exec_pJDB_export(sqlConn as string) 
    Dim conn As ADODB.Connection 
    Dim cmd As ADODB.Command 

    Dim iFrom, iTo As Integer 
    Dim stv As String 
    iFrom = 1999 
    iTo = 2002 
    stv = "1 TO 2 TON TRUCKS (COMMERCIAL)" 
    Set conn = New ADODB.Connection 

    conn.Open "DSN=Cars" 
    conn.ConnectionString = sqlConn 
    Set cmd = New ADODB.Command 
    cmd.ActiveConnection = conn 
    cmd.CommandType = adCmdStoredProc 
    cmd.CommandText = "pJDB_export" 

    cmd.Parameters.Append cmd.CreateParameter("@dteFrom", adInteger, adParamInput, , iFrom) 
    cmd.Parameters.Append cmd.CreateParameter("@dteTo", adInteger, adParamInput, , iTo) 
    cmd.Parameters.Append cmd.CreateParameter("@vehicle", adVarChar, adParamInput, 80, stv) 

    cmd.Execute 
    conn.Close 

End Function 
+3

你有什麼麻煩?代碼是否返回錯誤消息,或者您不確定如何訪問記錄?如果是後者,則需要[ADO記錄集](https://www.w3schools.com/asp/ado_ref_recordset.asp)。 –

+0

沒有任何內容正在返回並且沒有錯誤消息。不知道如何通過ADO訪問記錄 – Draco

+0

檢出ExecuteReader() https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executereader(v=vs.110).aspx – pmbAustin

回答

1

execute method返回一個recordset object。這個對象包含你的記錄。下面是例子:

Function Exec_pJDB_export(sqlConn As String) 
    Dim conn As ADODB.Connection 
    Dim cmd As ADODB.Command 
    Dim rs As ADODB.Recordset    ' ADO recordset object, for accessing records. 

    Dim iFrom, iTo As Integer 
    Dim stv As String 
    iFrom = 1999 
    iTo = 2002 
    stv = "1 TO 2 TON TRUCKS (COMMERCIAL)" 
    Set conn = New ADODB.Connection 

    conn.Open "DSN=Cars" 
    conn.ConnectionString = sqlConn 
    Set cmd = New ADODB.Command 
    cmd.ActiveConnection = conn 
    cmd.CommandType = adCmdStoredProc 
    cmd.CommandText = "pJDB_export" 

    cmd.Parameters.Append cmd.CreateParameter("@dteFrom", adInteger, adParamInput, , iFrom) 
    cmd.Parameters.Append cmd.CreateParameter("@dteTo", adInteger, adParamInput, , iTo) 
    cmd.Parameters.Append cmd.CreateParameter("@vehicle", adVarChar, adParamInput, 80, stv) 

    ' This line has changed. 
    Set rs = cmd.Execute() 


    ' Loops over the records. 
    Do Until rs.EOF 

     ' Display the contents of column one to the user. 
     MsgBox rs.Fields(0).Value 

     rs.MoveNext 
    Loop 



    rs.Close 
    conn.Close 
End Function 

快速概述:

  1. EOF代表文件結束的。當您查看所有記錄時,情況確實如此。
  2. 不要忘了打電話給MoveNext,否則do循環會一直持續下去!

rs.Fields(0).Value可以,如果你喜歡改爲rs.Fields("YourFieldName").Value。我發現通過名稱而不是位置來指代領域更容易。

+0

工作。最後一個問題。將其導出到excel或訪問表。一列長度小於500,另一列長度超過10000個字符。 – Draco

+0

工作。最後一個問題。我更喜歡將數據導出到表格,如果我必須將其轉儲到Excel。一列長度<500字符,另一列長度爲10000+字節。什麼是導出數據的最佳方式? 我寧願不必按列和行滾動記錄集。 謝謝 – Draco

+1

雖然VBA/ADO可以做到這一點,但有更簡單的方法。 [傳遞查詢](https://support.microsoft.com/en-gb/help/303968/how-to-create-an-sql-pass-through-query-in-access)可以將您的SP顯示爲表。作爲額外的獎勵,他們是免費的代碼。 –

相關問題