想要在插入数据库时​​知道错过的记录信息 [英] Want to know missed records information while inserting into database

查看:54
本文介绍了想要在插入数据库时​​知道错过的记录信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有要求请帮助我。我使用XML数据类型在SQL服务器数据库中插入大量的记录,我想知道由于任何不一致的数据是否错过任何记录插入,我想在日志文件中获取丢失的记录信息。如果有任何问题,请纠正我。

Hi All,

I have a requirement please help me. I am inserting bulk number of records in SQL server database using XML datatype, i want to know if due to any inconsistent data any of the records are missed to insert, i want to get that missed records information in a log file. Please correct me if any thing wrong.

推荐答案

嗨Ramesh,



你可以做一件事。



请将您的XML转换为SQL表,然后尝试使用except关键字查找缺失的记录。 except关键字将帮助您找到2个表之间的区别



示例:



Hi Ramesh,

You can do one thing.

Please convert your XML to SQL table and then try to find the missing record using "except" keyword. "except" keyword will help you to find the difference between 2 tables

Example:

declare @XML as XML = '<row>
    <col1>8</col1>
    <col2>3</col2>
    <col3>8</col3>
    <col4>5</col4>
</row>
<row>
    <col1>18</col1>
    <col2>5</col2>
    <col3>8</col3>
    <col4>5</col4>
</row>'

SELECT  
       Tbl.Col.value('Col1[1]', 'int'),  
       Tbl.Col.value('Col2[1]', 'int'),  
       Tbl.Col.value('Col3[1]', 'int'),
       Tbl.Col.value('Col4[1]', 'int')
FROM   @xml.nodes('//row') Tbl(Col) 





我们假设Col1列是您的主键。 (这将帮助您比较数据。您可以根据您的要求使用任何组合的列)



然后请以下面的方式比较数据:





Let's assume Col1 column is your primary key. (which will help you to compare the data. You can use any of the combination of column based on your requirement)

Then please compare data in below way:

SELECT
       Tbl.Col.value('Col1[1]', 'int')
FROM   @xml.nodes('//row') Tbl(Col)

except

select yourPrimaryColumn from YourTable





在输出中,您将获得上述查询中可用的数据,但可在以下查询中获得。



如果您有任何疑虑或疑问,请告知我们我错过了什么。



谢谢

Advay Pandya



In output, you will get data which are available on your above query but on available on your below query.

Please let me know if you have any concern or query or if I am missing something.

Thanks
Advay Pandya


这篇关于想要在插入数据库时​​知道错过的记录信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆