从Excel追加一行或多行到现有的SQL Server表

我已经将Excel表导入SQL Server 2016 Express,并使用导入向导创build了一个表。

现在当我更新我的Excel工作表时,例如添加一行或多行,我也想更新我的SQL Server表,也就是说,向表中添加一行或多行。 什么是最简单的方法来做到这一点? 以“追加”行的方式。 我不想再添加整个Excel表格

你问最简单的解决scheme。 由于您已经习惯使用向导,所以在我看来,最简单的方法是使用向导将“已更新”的Excel工作表/文件导入到SQL Server Express中。 然后,将它导入到一个新的表格(不删除旧的表格)。

之后,插入新行或使用简单的SQL MERGE语句更新SQL Server上的现有logging。 之后,您可以再次删除/删除导入的表格(因为现有表格已更新)。

虽然我不知道你的表,下面的SQL代码示例显示了一个简单的合并基本客户表,其中tblCustomers将退出表(要更新/插入新行), tblCustomersNEW将是新的导入(将被删除一旦更新/追加完成):

 merge dbo.tblCustomers as target using dbo.tblCustomersNEW as source on source.ID = target.ID when matched then update set target.Name = source.Name, target.Country = source.Country when not matched by target then insert (Name, Country) values ( source.Name, source.Country ); 

请注意,在开始之前, MERGE语句在末尾需要一个类似于CTE需要分号的分号; With myTable as... ; With myTable as...

有关MERGE语句的更多信息,可以参考MSDN上的文章: https : //msdn.microsoft.com/en-us/library/bb510625.aspx