使用时间值从Excel中删除SQL行

时间:2017-08-18 14:47:23

标签: sql-server excel vba excel-vba scripting

当列将TIME作为数据类型时,我无法在此处使用此功能。当我在没有TIME作为数据类型的不同表上使用它时,这可以工作。 我希望能够使用以下脚本在特定表中DELETE SQL行。

Sub Button1_Click()
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command

Dim strSQL As String

strSQL = "DELETE FROM dbo.TimeLog WHERE EventDate = ? AND ID = ? AND DeptCode = ? AND OpCode = ? AND StartTime = ? AND FinishTime = ? AND Units = ?;"


Set conn = New ADODB.Connection
conn.Open "Provider=SQLOLEDB;Data Source=BLANK;Initial Catalog=BLANK;User ID=BLANK;Password=BLANK;"
'Skip the header row
iRowNo = 2


With Sheets("Sheet1")
  'Loop until empty cell in FirstName
  Do Until .Cells(iRowNo, 1) = ""

  Set cmd = New ADODB.Command

cmd.ActiveConnection = conn
cmd.CommandType = adCmdText
cmd.CommandText = strSQL

  cmd.Parameters.Append _
    cmd.CreateParameter("pEventDate", adVarChar, adParamInput, 15, .Cells(iRowNo, 1))
  cmd.Parameters.Append _
    cmd.CreateParameter("pID", adInteger, adParamInput, , .Cells(iRowNo, 2))
  cmd.Parameters.Append _
    cmd.CreateParameter("pDeptCode", adVarChar, adParamInput, 2, .Cells(iRowNo, 3))
  cmd.Parameters.Append _
    cmd.CreateParameter("pOpCode", adVarChar, adParamInput, 2, .Cells(iRowNo, 4))
  cmd.Parameters.Append _
    cmd.CreateParameter("pStartTime", adDBTime, adParamInput, 0, .Cells(iRowNo, 5))
  cmd.Parameters.Append _
    cmd.CreateParameter("pFinishTime", adDBTime, adParamInput, 0, .Cells(iRowNo, 6))
  cmd.Parameters.Append _
    cmd.CreateParameter("pUnits", adInteger, adParamInput, , .Cells(iRowNo, 7))

  cmd.Execute

  iRowNo = iRowNo + 1
  Loop

  MsgBox "Data Successfully Deleted"

End With

conn.Close
Set conn = Nothing
End Sub

这是我收到的错误代码。

`Run-time error '-2147217900 (80040e14)':
 The data types time and datetime are incompatible in the equal to 
 operator`

它必须是基本的东西,但我没有看到它,任何帮助都会受到赞赏。,谢谢

0 个答案:

没有答案