简体   繁体   中英

Double records in result set

I have found a strange phenomena on MSSQL server.

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.

For me it seems to be, that the Updated datetime value was changed and it was included one more time in result set. But is it possible?

UPDATE: Source code I using for downloading data from 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

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 , ...). Please manually check inside your table (in MSSQL Server rather than inside a function or method) to see if there is dupplicated IDs. 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.

You may also try this query to see if it gets dupplicated IDs inside your context:

SELECT ID
from testTable
ORDER BY ID

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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