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 ステートメントの最初の 2 つのチャンクが冗長であるように見えます。もしそれでも

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

また

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

false として評価される場合、where 句全体が単純に次のように評価されます。

( v_final_transactions.contact_id ) = [rcampaigninclusion!contact_id]

ところで、WHERE 句のすべてのフィールド名を括弧で囲む必要はありませんか? それらを除外すると、はるかに簡単になります。

于 2013-05-29T00:24:40.880 に答える