将vba中的Date变量传递给sql语句

时间:2015-08-07 07:54:15

标签: sql-server excel vba date variables

我已经查看了已发布的各种不同场景,但没有一个解决方案似乎带来任何乐趣。 我试图将日期变量传递给sql select语句但是我给出了关于转换为日期/时间的错误或者变量未被正确定义的事实,即错误的语法。 这同样适用于变量' contractNumber'但我希望日期变量完成,同样适用于' contractNumber'变量。 我试过用""来围绕变量。 /"& /#"" &安培;但这些组合似乎都不起作用......

我正在使用的代码如下," Control"表格是英国标准日期 - DD / MM / YYYY

Sub MEHT()

Dim startdate As Date
Dim enddate As Date
Dim contractNumber As String

startdate = Sheets("Control").Range("K8").Value
enddate = Sheets("Control").Range("K10").Value
contractNumber = Sheets("Control").Range("K13").Value

Call ConnectSqlServer

End Sub

Sub ConnectSqlServer()

    Dim conn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim sConnString As String

    ' Create the connection string.
    sConnString = "Provider=SQLOLEDB;Data Source=NT124431\DW;" & _
                  "Initial Catalog=hdw2;" & _
                  "Integrated Security=SSPI;"


    ' Create the Connection and Recordset objects.
    Set conn = New ADODB.Connection
    Set rs = New ADODB.Recordset

    ' Open the connection and execute.
    conn.Open sConnString
    Set rs = conn.Execute("SELECT SAF.Tracking_Number, ORH.CUSTOMER_PO_NUMBER,MAX(DOC.DOCUMENT_NUMBER),DOC.DOCUMENT_DATE_KEY,SAF.ITEM_NUMBER,  MAX(ITM.HOMECRAFT_ITEM_NUMBER), " _
        & "MAX(ITM.ITEM_DESCR),MAX(ADR.ADDR_NAME),MAX(ADR.ADDR1),MAX(ADR.ZIP_5_KEY),SUM(SAF.QUANTITY_SOLD),SUM(SAF.EXTENDED_AMOUNT) " _
        & ",SUM(SAF.EXTENDED_AMOUNT) / SUM(SAF.QUANTITY_SOLD) FROM SALES_FACT SAF  JOIN LU_CUSTOMER_TBL CUS ON SAF.CUSTOMER_KEY = CUS.CUSTOMER_KEY " _
        & "JOIN ORDER_DM ORH ON SAF.ORDER_KEY = ORH.ORDER_KEY JOIN PRODUCT_DM PRD ON SAF.PRODUCT_KEY = PRD.PRODUCT_KEY JOIN DOCUMENT_DM DOC " _
        & "ON SAF.DOCUMENT_KEY = DOC.DOCUMENT_KEY JOIN LU_ITEM ITM ON SAF.ITEM_NUMBER = ITM.ITEM_NUMBER JOIN LU_ADDRESS ADR ON DOC.SHIP_TO_ADDRESS_KEY = ADR.ADDRESS_KEY " _
        & "WHERE ITM.ITEM_CATEGORY_KEY NOT IN ('31010') AND SAF.TRACKING_CODE NOT IN ('F') AND PRD.PRODUCT_LINE_KEY NOT IN ('UN') AND PRD.PRODUCT_SUB_LINE_KEY NOT IN ('60P') " _
        & "AND SAF.TRACKING_NUMBER = " & contractNumber & " AND SAF.EXTENDED_AMOUNT > 0 AND DOC.DOCUMENT_DATE_KEY BETWEEN CONVERT(Date, " & startdate & " , 120) AND CONVERT(Date," & enddate & " , 120) " _
        & "GROUP BY ORH.CUSTOMER_PO_NUMBER, DOC.DOCUMENT_DATE_KEY,SAF.ITEM_NUMBER,SAF.Tracking_Number ORDER BY SAF.Tracking_Number,DOC.DOCUMENT_DATE_KEY")

    ' Check we have data.
    If Not rs.EOF Then
        ' Transfer result.
        Sheets("Detail").Range("A1").CopyFromRecordset rs
    ' Close the recordset
        rs.Close
    Else
        MsgBox "Error: No records returned.", vbCritical
    End If

    ' Clean up
    If CBool(conn.State And adStateOpen) Then conn.Close
    Set conn = Nothing
    Set rs = Nothing

End Sub

1 个答案:

答案 0 :(得分:1)

  

我正在使用的代码如下," Control"床单   是标准的英国日期 - DD / MM / YYYY

尽管是英国同胞,我不会建议使用你的日期格式......

将日期传递给SQL Server最安全的方法是使用" yyyy-MM-dd"。无论您身在何处,这都可以保证它的正常运作。

select * from [SomeTable]
where Update_Time >= '2015-08-06'