请教insert语句插入差异数据的问题
目前有一个excel表格和一个SQL数据库表AA
excel包含更多记录,想要用insert语句向AA插入excel表格中比数据表AA多的数据,也就是插入数据,假如记录内容一致的就不添加
当然也可以用完全覆盖,但是问题是excel转换过来的数据会自动变成nvarchar类型,我不想改变数据表的结构
请问insert语句能直接实现这个功能吗?
[解决办法]
分为两步 处理
先得到excel的数据 和 数据库中的数据进行左连接,update可以连接到的数据,
然后insert 连接不到的数据
[解决办法]
insert tab (...)select ...from tab转 awhere not exists ( select 1 from tab where col1 = a.col1 and col2 = a.col2 and ... )
[解决办法]
--插入的时候转换字段类型,并排除已有的 insert into tb select cast(字段 as int),... from excel a where not exists(select 1 from tb where id=a.id)
[解决办法]
程序中的话只能读一条然后比对一次,或者将excel内容读到datatable中,将数据库表中的内容也读到一个datatable中,然后再进行比较
[解决办法]
思路:先导入到一个临时表,再利用临时表插入到主表,再删除临时表