2017-07-17 118 views
0

我嘗試了每次迭代都可以在網上找到,但我仍然返回-1^爲我的記錄集計數,而不是實際計數。我嘗試了CursorType, LockType, & CursorLocation的多種組合。這是我的代碼。記錄集計數返回-1

Sub test() 
    Dim FullQry As String 
    Dim qry1 As String 
    Dim qry2 As String 
    Dim qry3 As String 
    Dim qry4 As String 

'DECLARE VARIABLES FOR CONNECTION (HOW THE QUERY CONNECTS TO TERADATA) 
    Dim cn As ADODB.Connection 
    Set cn = New ADODB.Connection 

'DECLARE VARIABLES FOR RECORDSET (THE RESULTS OF THE SQL QUERY) 
    Dim rs As ADODB.Recordset 
    Set rs = CreateObject("ADODB.Recordset") 

'DECLARE VARIABLES FOR COMMAND (I THINK THIS MAKES TERADATA RUN THE QUERY AFTER A CONNECTION IS ESTABLISHED) 
    Dim cmdSQLData As ADODB.Command 
    Set cmdSQLData = New ADODB.Command 

'Connect to Teradata 
    cn.Open "Data Source = MOSAIC_PROD; Database= prod_flight_ops_combined_vw; Persist Security info=True; User ID=758673; Password=PSPL444eae???; Session Mode=System Default;" 
    Set cmdSQLData.ActiveConnection = cn 
    rs.CursorType = adOpenStatic 
    rs.LockType = adLockReadOnly 
    rs.CursorLocation = adUseClient 

'Define Qry 

    qry1 = "SELECT AIRLINE, FLT_NUM, SKD_ORIG, SKD_DEST, ACT_ORIG, ACT_DEST, SKD_TAIL, ACT_TAIL, SKD_SUBFLEET, ACT_SUBFLEET, SKD_OUT_GMT, SKD_IN_GMT,  ACT_OUT_GMT,  ACT_ON_GMT,  ACT_IN_GMT, ACT_OUT_DATE_GMT, ACT_IN_DATE_GMT, ACT_OFF_GMT, " 
    qry2 = "SKD_OUT_DATE_GMT , SKD_IN_DATE_GMT, SKD_BLK, ACT_BLK, SKD_AIR, ACT_AIR, SKD_TXOT, ACT_TXOT, SKD_TXIN, ACT_TXIN, SKD_OFF, ACT_OFF, SKD_ON, ACT_ON, SKD_TURN, ACT_TURN, AVAIL_TURN, MOGT, OP_STATUS, OP_STATUS_DESC, SUB_DIVERT_DESC , DELAY_MSG " 
    qry3 = "FROM prod_flight_ops_combined_vw.OPS_FLIGHT_LEG " 
    qry4 = "WHERE act_out_date_gmt > current_date - 45 and act_in_date_gmt < current_date - 1 and Airline = 'AA';" 

FullQry = qry1 & qry2 & qry3 & qry4 


    cmdSQLData.CommandText = FullQry 
    cmdSQLData.CommandType = adCmdText 
    cmdSQLData.CommandTimeout = 0 
    Set rs = cmdSQLData.Execute() 
    x = rs.RecordCount 


Set rs = Nothing 
Set cmdSQLData = Nothing 
cn.Close 
End Sub 

感謝您的幫助。

+0

什麼是數據庫類型? –

+1

您可能會發現,無論您使用的是何種RDBMS及其驅動程序,都無法實現。如果您發現這種情況,那麼您可以發送一個計數(*)查詢來獲取記錄數。也可以向上發送兩個SQL語句(在大多數數據庫中)用分號分隔,然後用'rs.nextrecordset'迭代存儲在'rs'中的記錄集。另外...根據RDBMS,您可以添加一個新字段來存儲完整的記錄集計數,並將字段COUNT(*)OVER()作爲AllRecordCount添加到您現有的SELECT中。 – JNevill

回答

0
Sub test() 
    Dim cn As ADODB.Connection 
    Set cn = New ADODB.Connection 
    cn.Open "Data Source=MOSAIC_PROD" 

    Dim rs As ADODB.Recordset 
    Set rs = CreateObject("ADODB.Recordset") 

    rs.CursorLocation = adUseClient 

    rs.Open "SELECT * from FROM prod_flight_ops_combined_vw.OPS_FLIGHT_LEG", cn 

    MsgBox (rs.RecordCount) 


    cn.Close 
    Set rs = Nothing 
    Set cn = Nothing 
End Sub