I’m using sql-server 2005 and ASP.NET with C#.
I have Users table with
userId(int),
userGender(tinyint),
userAge(tinyint),
userCity(tinyint)
(simplified version of course)
I need to select always two fit to userID I pass to query users of opposite gender, in age range of -5 to +10 years and from the same city.
Important fact is it always must be two, so I created condition if @@rowcount<2 re-select without age and city filters.
Now the problem is that I sometimes have two returned result sets because I use first @@rowcount on a table. If I run the query.
Will it be a problem to use the DataReader object to read from always second result set? Is there any other way to check how many results were selected without performing select with results?
Can you simplify it by using
SELECT TOP 2?Update: I would perform both selects all the time, union the results, and then select from them based on an order (using
SELECT TOP 2) as the union may have added more than two. Its important that this next select selects the rows in order of importance, ie it prefers rows from your first select.Alternatively, have the reader logic read the next result-set if there is one and leave the SQL alone.