从EXCEL更新SQL SERVER数据库

我有一个Excel数据表:

1. Inventory item number 2. Description 3. Inventory Database ID (PRIMARY KEY) 

我有大约1000行。 我想删除数据库中与我的Excel列表中的项目编号相匹配的项目编号。 我可以编写一个应用程序来做到这一点,但似乎太复杂了。

有没有一种简单的方法通过Excel或SQL Server运行一个SQL语句删除我的Excel表中的项目编号与创build应用程序的麻烦?

为了快速更新。 我觉得这是最好的方法。

将一列添加到Excel中,并将公式更新语句构造为:

 ="DELETE Table1 WHERE ItemNumber='"&A1&"' AND InventoryId = "&C1 

复制公式,并将结果复制/粘贴到SQL窗口中并运行。

专家提示,如果你有很多的撇号处理,它可能是值得的,事先做一个全球性的查找/replace。 或者你可以从公式中处理它们。 即:

 ="DELETE Table1 WHERE ItemNumber='"&SUBSTITUTE(A1,"'","''")&"' AND InventoryId = "&C1 

如果你不想通过一个SQL接口,你可以在更新连接string后运行附加的代码。

 Sub ExecuteSQLCommand() 'Execute the SQL string passed through Dim conn As ADODB.Connection, strConn As String, sSQLCommand As String Dim cmd As ADODB.Command, lLoop As Long, lLastRow As Long Set conn = New ADODB.Connection conn.ConnectionString = "Provider=SQLOLEDB;" & _ "Data Source=DCC08SQL;" & _ "Initial Catalog=HarvestPress;" & _ "Integrated Security=SSPI;" & _ "Database=HarvestPress;" & _ "Trusted_Connection=True;" conn.Open Set cmd = New ADODB.Command lLastRow = Cells(Rows.Count, 1).End(xlUp).Row With cmd .ActiveConnection = conn .CommandType = adCmdText For lLoop = 1 To lLastRow sSQLCommand = "DELETE FROM Table1 WHERE ItemNumber='" & Cells(lLoop, 1) & "' AND InventoryId = " & Cells(lLoop, 1) .CommandText = sSQLCommand .Execute Next lLoop End With conn.Close Set conn = Nothing Set cmd = Nothing End Sub