I’m having problems with a table copy erroring due to a primary key constraint when the primary key is on the new table. In doing some investigation by not setting a primary key I was able to find a duplicate row in my source table, still unsure how it got in there but what I’m more curious about is the rest of what I found.
Here goes:
I have a SQL query that selects the full table and returns the following:
SELECT *
FROM search_term_suggest
order by search_term
you can see the value is duplicated and I had to scroll through the table to find it.

But when I limit my query to:
SELECT *
FROM search_term_suggest
where search_term = 'b'
you can see it filters out the first value. There are no triggers on the database or anything I can see that would limit the select query.

I’m running SQL 2008 r2. Any help is greatly appreciated. The database I’m copying the table to is a SQL 2000 database and when selecting using the same queries as above I get the results I would expect.
EDIT: search_term is a varchar(100), search_term_suggest is a table not a view.
My guess:
search_termis aCHAR/NCHARand has trailing spaces (or trailing spaces are being considered for other reasons). Try:You may also need to eliminate tabs/carriage returns, which aren’t affected by trim operations.
Note that both of the above queries preclude using an index, so if there is an index on
search_termyou should probably rebuild it after changing the column tovarcharornvarcharand making sure that padding settings and whitespace aren’t causing the problem.Other guesses: as @Dems points out, check the length of both columns on the first query. Also try changing your where clause to the following, for investigation purposes only:
If you get any result there, then you can say:
This will help you troubleshoot what is in that column aside from the
b– it may be whitespace you haven’t checked for (e.g. a tab or carriage return) or other non-printing characters.