I am using SQL Server 2008 R2.
I'm not sure if I discovered a strange SQL quirk, or (more likely) something in my code causes this strange behavior, especially since Google did not find anything. I have a view called vwResponsible_Office_Address.
SELECT * FROM vwResponsible_Office_Address
.. returns 403 rows
This code:
SELECT TOP 1000 * FROM vwResponsible_Office_Address
.. returns 409 rows, since it includes 6 duplicates.
However, this:
SELECT TOP 1000 * FROM vwResponsible_Office_Address ORDER BY ID
.. returns 403 rows again.
I can post code for the view if it matters, but does it make sense for SELECT TOP to work this way? I understand that SELECT TOP can return records in any order, but I donโt understand why the number of returned records should change.
Does the view use cross apply, which might affect the result set in some way?
EDIT: View Definition on Demand
CREATE VIEW [dbo].[vwResponsible_Office_Address] AS SELECT fp.Entity_ID [Reg_Office_Entity_ID], fp.Entity_Name [Reg_Office_Entity_Name], addr.Address_ID FROM [dbo].[Entity_Relationship] er INNER JOIN [dbo].[Entity] fp ON er.[Related_Entity_ID] = fp.[Entity_ID] INNER JOIN [dbo].[Entity_Address] ea ON ea.[Entity_ID] = fp.[Entity_ID] CROSS APPLY ( SELECT TOP 1 Address_ID FROM [dbo].[vwEntity_Address] vea WHERE [vea].[Entity_ID] = fp.Entity_ID ORDER by ea.[Address_Type_ID] ASC, ea.[Address_ID] DESC ) addr WHERE [Entity_Relationship_Type_ID] = 25
source share