简体   繁体   English

在结果集中重复记录

[英]Double records in result set

I have found a strange phenomena on MSSQL server. 我在MSSQL服务器上发现了一个奇怪的现象。

Let say we have a table: 假设我们有一张桌子:

CREATE TABLE [testTable]
(
     [ID] [numeric](11, 0) NOT NULL,
     [Updated] [datetime] NULL,
     PRIMARY KEY (ID)
);

I do a simple select based on Updated field: 我根据已更新字段进行简单选择:

SELECT TOP 10000 ID, Updated
FROM testTable
WHERE Updated>='2013-05-22 08:55:12.152'
ORDER BY Updated

And now comes the fun part: how can I have in result set double records - I mean same ID in 2 records with different Updated value. 现在来了有趣的部分:如何在结果集中创建双记录-我的意思是2条记录中的ID相同,但更新值不同。

For me it seems to be, that the Updated datetime value was changed and it was included one more time in result set. 对我来说,似乎更新了datetime值已更改,并且在结果集中又包含了一个时间。 But is it possible? 但是有可能吗?

UPDATE: Source code I using for downloading data from SQL server: 更新:我用于从SQL Server下载数据的源代码:

using (SqlCommand cmd = new SqlCommand(sql, Connection) { CommandTimeout = commandTimeout })
{
    using (System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(cmd))
    {
        DataTable retVal = new DataTable();
        adapter.Fill(retVal);
        return retVal;
    }
}

Connection = SqlConnection 连接= SqlConnection

sql = "SELECT TOP 10000 ...." sql =“ SELECT TOP 10000 ....”

Your question seems to lack some details but here's my ideas. 您的问题似乎缺少一些细节,但这是我的想法。

The first case I'd think of would be that you are somehow selecting those IDs twice (could be a join , group by , ...). 我想到的第一种情况是您以某种方式两次选择了这些ID(可能是joingroup by ,...)。 Please manually check inside your table (in MSSQL Server rather than inside a function or method) to see if there is dupplicated IDs. 请手动检查表内部(在MSSQL Server中,而不是在函数或方法中),以查看是否有重复的ID。 If there is, the issue is that your Primary Key hasn't been set correctly. 如果存在,则问题在于您的主键设置不正确。 Otherwise, you will need to provide all the relevant code that is used to select the data in order to get more help. 否则,您将需要提供用于选择数据的所有相关代码,以获得更多帮助。

Another case might be that someone or something altered the primary key so it is on both ID and Updated , allowing the same ID to be inserted twice as long as the Updated field doesn't match too. 另一种情况可能是某人或某物更改了主键,因此它既在ID又在Updated ,只要Updated字段也不匹配,就允许插入相同的ID两次。

You may also try this query to see if it gets dupplicated IDs inside your context: 您也可以尝试使用此查询来查看它是否在上下文中获得重复的ID:

SELECT ID
from testTable
ORDER BY ID

I hope this helps. 我希望这有帮助。

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

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