简体   繁体   English

通过覆盖将Excel表导入到SQL

[英]Import Excel table to SQL by overwritting

I have an Excel file that maps exactly to a table in SQL Server. 我有一个Excel文件,它完全映射到SQL Server中的表。 I have tried to import but I get the UNIQUE KEY error. 我尝试导入,但是出现UNIQUE KEY错误。

How can I overwrite the exisitng values in the database table with those in the excel file? 如何用Excel文件中的值覆盖数据库表中的exisitng值? I can convert to csv. 我可以转换为csv。 if this is any help. 如果有帮助的话。 Is there a statement I can write to do this? 我可以写一个声明来做到这一点吗?

Any guidance would be much appreciated. 任何指导将不胜感激。

Thank you 谢谢

Please display the whole ERRO. 请显示整个ERRO。 if you have UNIQUE KEY Error then you have to create update query using primary key. 如果您有UNIQUE KEY错误,则必须使用主键创建更新查询。 you might have to write TSQL query. 您可能必须编写TSQL查询。

  1. Create a second table using the same DDL as that table (in the database). 使用与该表相同的DDL(在数据库中)创建第二个表。

  2. Afterward, import the Excel file into that newly created table. 之后,将Excel文件导入到该新创建的表中。

  3. Delete from the actual table (not the one just created above) all rows where the columns making up the unique key constraint match that of the the rows newly inserted into the newly created table. 从实际表(不是上面刚刚创建的表)中删除所有行,其中构成唯一键约束的列与新插入到新创建的表中的行的列匹配。

  4. Then insert into the actual table all rows that exist in the second, newly created and populated table. 然后,将实际存在于第二个新创建并填充的表中的所有行插入到实际表中。 Because you just deleted the rows with the values you want to overwrite, you will no longer violate the unique constraint. 因为您只是删除了具有要覆盖的值的行,所以您将不再违反唯一约束。

If you provide the column names of your table and the column(s) that make up the unique constraint I can help you write the query for #3 if needed. 如果您提供表的列名和构成唯一约束的列,那么我可以根据需要帮助您编写针对#3的查询。

Step 3 would be something like: 步骤3类似于:

delete from table_name
 where unique_id in
       (select unique_id from newly_created_table_in_step1_and_2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM