0

目前我收到错误“Build WHERE -> insert_fields()”。此代码发生此错误:

Do Until rCurrent.EOF 
    rCurrent.MoveNext
Loop

rCurrent 是一个 ADODBRecordSet,它被赋予了以下值:

sSQL = ""
sSQL = sSQL & "SELECT v_final_transactions.* " & vbCrLf
sSQL = sSQL & "FROM   v_final_transactions " & vbCrLf
sSQL = sSQL & "WHERE  ( " & vbCrLf
sSQL = sSQL & "       ( " & vbCrLf
sSQL = sSQL & "( v_final_transactions.contact_id ) = " & rcampaigninclusion!contact_id & " ) " & vbCrLf
sSQL = sSQL & "AND " & vbCrLf
sSQL = sSQL & "( " & vbCrLf
sSQL = sSQL & "( v_final_transactions.dbd_segment ) = '" & rCampaignInclusion!PRODUCT_SEGMENT_1 & "' ) ) " & vbCrLf
sSQL = sSQL & " OR ( " & vbCrLf
sSQL = sSQL & "( ( v_final_transactions.contact_id ) = " & rcampaigninclusion!contact_id & " ) " & vbCrLf
sSQL = sSQL & "AND " & vbCrLf
sSQL = sSQL & "( " & vbCrLf
sSQL = sSQL & "( v_final_transactions.dbd_segment ) = '" & rCampaignInclusion!PRODUCT_SEGMENT_2 & "' ) ) " & vbCrLf
sSQL = sSQL & " OR " & vbCrLf
sSQL = sSQL & "(( ( v_final_transactions.contact_id ) = " & rcampaigninclusion!contact_id & " ))"        
Set rCurrent = New ADODB.Recordset
rCurrent.Open sSQL, cDS, adOpenStatic, adLockOptimistic, adCmdText

最终的 SQL 如下所示:

SELECT V_FINAL_TRANSACTIONS.* 
FROM V_FINAL_TRANSACTIONS 
WHERE ( 
      ( 
( V_FINAL_TRANSACTIONS.CONTACT_ID ) = 1036219 ) 
AND 
( 
( V_FINAL_TRANSACTIONS.DBD_SEGMENT ) ='Acrobat' ) ) 
 OR ( 
( ( V_FINAL_TRANSACTIONS.CONTACT_ID ) = 1036219 ) 
AND 
( 
( V_FINAL_TRANSACTIONS.DBD_SEGMENT ) = '' ) ) 
 OR 
(( ( V_FINAL_TRANSACTIONS.CONTACT_ID ) = 1036219 ))

这是运行的逻辑:

If rCurrent.Properties.Count > 0 Then

    If (rCurrent.EOF = False) Then
        rCurrent.MoveFirst

    End If

    Do Until rCurrent.EOF
        dTransDate = rCurrent!TRANSACTION_DATE

        If (rCampaignInclusion!CAMPAIGN_ID = 446) And (dTransDate >= rCampaignInclusion!DATE_UPDATE) And (dTransDate <= (rCampaignInclusion!DATE_UPDATE + 90)) Then

            iDifference = dTransDate - rCampaignInclusion!DATE_UPDATE
            iCampaignHistory = rCampaignInclusion!CAMPAIGN_HISTORY_ID

            If rCurrent!REVENUE > 0 Then
                iTransactionRevenue = rCurrent!REVENUE
            Else
                iTransactionRevenue = 0
            End If


            sSQLMatched = "INSERT INTO TMP_MATCH_CON_HISTORY_UPDATE ( TRANSACTION_08_ID, CAMPAIGN_HISTORY_ID, DATE_UPDATE, RESULT_VALUE_2, NO_DAYS) SELECT " & rCurrent!TRANSACTION_08_ID & ", " & iCampaignHistory & ", '" & Year(Date) & "-" & Month(Date) & "-" & Day(Date) & "', " & iTransactionRevenue & ", " & iDifference & ";"
            cDS.Execute (sSQLMatched)

            'rCurrent.Edit
            'rCurrent!MATCHED = "Y"
            'rCurrent.Update

            sSQLMatched = "UPDATE TMP_MATCH_TRANSACTIONS SET MATCHED='Y' WHERE TMP_MATCH_TRANSACTIONS.TRANSACTION_08_ID=" & rCurrent!TRANSACTION_08_ID
            cDS.Execute (sSQLMatched)


        ElseIf (dTransDate >= rCampaignInclusion!CAMPAIGN_DATE_START) And (dTransDate <= rCampaignInclusion!CAMPAIGN_DATE_END) Then
            iDifference = dTransDate - rCampaignInclusion!CAMPAIGN_DATE_START
            iCampaignHistory = rCampaignInclusion!CAMPAIGN_HISTORY_ID


            If rCurrent!REVENUE > 0 Then
                iTransactionRevenue = rCurrent!REVENUE
            Else
                iTransactionRevenue = 0
            End If

            sSQLMatched = "INSERT INTO TMP_MATCH_CON_HISTORY_UPDATE ( TRANSACTION_08_ID, CAMPAIGN_HISTORY_ID, DATE_UPDATE, RESULT_VALUE_2, NO_DAYS) SELECT " & rCurrent!TRANSACTION_08_ID & ", " & iCampaignHistory & ", '" & Year(Date) & "-" & Month(Date) & "-" & Day(Date) & "', " & iTransactionRevenue & ", " & iDifference & ";"
            cDS.Execute (sSQLMatched)


            'rCurrent!MATCHED = "Y"
            'rCurrent.Update

            sSQLMatched = "UPDATE TMP_MATCH_TRANSACTIONS SET MATCHED='Y' WHERE TMP_MATCH_TRANSACTIONS.TRANSACTION_08_ID=" & rCurrent!TRANSACTION_08_ID
            cDS.Execute (sSQLMatched)


        End If

        rCurrent.MoveNext

    Loop

所以我的问题是我的语法有什么问题?

4

1 回答 1

1

您确实需要提供完整的错误消息(包括任何错误编号)以帮助诊断问题。

在我们等待的同时,我稍微简化了您的 SQL:

SELECT v_final_transactions.* 
FROM   v_final_transactions 
WHERE
( 
    ( 
        ( v_final_transactions.contact_id ) = [rcampaigninclusion!contact_id]
    ) 
    AND 
    ( 
        ( v_final_transactions.dbd_segment ) = '[rCampaignInclusion!PRODUCT_SEGMENT_1]'
    )
) 
OR
( 
    (
        ( v_final_transactions.contact_id ) = [rcampaigninclusion!contact_id]
    )
    AND 
    ( 
        ( v_final_transactions.dbd_segment ) = '[rCampaignInclusion!PRODUCT_SEGMENT_2]'
    )
) 
OR 
(
    (
        ( v_final_transactions.contact_id ) = [rcampaigninclusion!contact_id]
    )
)

看这个,似乎顶级 WHERE 语句的前两块是多余的。即使

( v_final_transactions.dbd_segment ) = '[rCampaignInclusion!PRODUCT_SEGMENT_1]'

或者

( v_final_transactions.dbd_segment ) = '[rCampaignInclusion!PRODUCT_SEGMENT_2]'

评估为假,整个 where 子句评估为:

( v_final_transactions.contact_id ) = [rcampaigninclusion!contact_id]

顺便说一句,您肯定不需要将 WHERE 子句中的每个字段名都用括号括起来吗?如果你把它们排除在外,它会容易得多。

于 2013-05-29T00:24:40.880 回答