Invalid SQL statement expected; ‘DELETE’,’INSERT’,

后端 未结 1 1790
孤独总比滥情好
孤独总比滥情好 2021-01-26 13:56

I have a code

Dim Cn As New ADODB.Connection

Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
Dim i As Long

Cn.ConnectionString = \"Provider=Microsoft.Jet         


        
相关标签:
1条回答
  • 2021-01-26 14:37

    The syntax for your parametered query doesn't look right.

    DECLARE @TS_HD DOUBLE;
    SET @TS_HD = ?TS_HD;
    SELECT 
         So_HD
        ,Ngay_HD
        ,Ten_Khach_Hang
        ,Ma_So_Thue
        ,SUM(Doanh_So_KT) AS DoanhSo
        ,SUM(Thue_VAT) AS VAT 
    FROM [BR$]
    WHERE Ngay_HD <> NULL AND TS_HD = @TS_HD 
    GROUP BY 
         So_HD
        ,Ma_So_Thue
        ,Ngay_HD
        ,Ten_Khach_Hang
    ORDER BY 
         Ngay_HD
    

    I'm not sure the provider supports DECLARE statements. If it doesn't, that's why you're getting this message. Let's try to remove it:

    SELECT 
         So_HD
        ,Ngay_HD
        ,Ten_Khach_Hang
        ,Ma_So_Thue
        ,SUM(Doanh_So_KT) AS DoanhSo
        ,SUM(Thue_VAT) AS VAT 
    FROM [BR$]
    WHERE Ngay_HD <> NULL AND TS_HD = ?
    GROUP BY 
         So_HD
        ,Ma_So_Thue
        ,Ngay_HD
        ,Ten_Khach_Hang
    ORDER BY 
         Ngay_HD
    

    Notice the WHERE clause, WHERE Ngay_HD <> NULL AND TS_HD = ? - the parameter placeholder is just a question mark.

    I'm pretty sure that statement would work.

    I'm less sure about how the parameter is passed in though:

    Set TS_HD = cmd.CreateParameter("?TS_HD", adDouble, adParamInput)
    

    I think you can drop the name parameter, it's optional - and some providers don't support named parameters. That sucks though, because now you need to name the arguments since the Name parameter is first in the signature of CreateParameter:

    Set TS_HD = cmd.CreateParameter(Type:=adDouble, Direction:=adParamInput)
    

    An alternative could be to New it up and initialize it "manually":

    Dim param As New ADODB.Parameter
    With param
        .Type = adDouble
        .Direction = adParamInput
        .Value = 0.01    'in excel this value is 10% '?? really? it's 1% here!
    End With
    
    cmd.Parameters.Append param
    
    0 讨论(0)
提交回复
热议问题