各位程式高手,我要將Excel表中的資料匯入 MSSQL裡,文字、數字都可以正常匯入,可是其中一欄為日期型態的就無法正常匯入。
語法如下 :
For i = 2 To Range("A" & Cells.Rows.Count).End(xlUp).Row
strSQL = "INSERT INTO zScProd(BillNo,CustID,ItemNO,ProductName,Quantity,DailyProdtQty,OuterDATE,NewOuterDATE) Values('" & Cells(i, 1) & "','" & Cells(i, 2) & "','" & Cells(i, 3) & "','" & Cells(i, 4) & "'," & Cells(i, 5) & "," & Cells(i, 6) & ",'" & Cells(i, 7) & "',#" & Cells(i, 8) & "#) "
其中 NewOuterDATE 為日期欄位,SQL資料型態為datetime,在" & Cells(i, 8) & "左右邊加#號,執行時會出現 "接近#之處的語法不正確"。
不加#號,2014/06/16匯進去後會變成1900-01-21
請問加#符號不是代表日期嗎??語法哪裡錯誤了請各位高手解答!~~感謝
hanker提到:
strSQL = "INSERT INTO zScProd(BillNo,CustID,ItemNO,ProductName,Quantity,DailyProdtQty,OuterDATE,NewOuterDATE) Values('" & Cells(i, 1) & "','...(恕刪)
換成數值後再匯入不知是否可行
<pre class="c" name="code">
strSQL = "INSERT INTO zScProd(BillNo,CustID,ItemNO,ProductName,Quantity,DailyProdtQty,OuterDATE,NewOuterDATE) Values('" & Cells(i, 1) & "','" & Cells(i, 2) & "','" & Cells(i, 3) & "','" & Cells(i, 4) & "'," & Cells(i, 5) & "," & Cells(i, 6) & ",'" & Cells(i, 7) & "'," & Format(Cells(i, 8,"#") & ") "