I am selecting an int value from two different tables as shown below
select col1 from tablea
union
select col1 from tableb
The requirement is if result is found in first query, use that; otherwise, look in second table.
select top 1 * from (select col1 from tablea union select col1 from tableb) as a
Problem is the highest numerical value of the result set is returned as top 1 – not the first result found.
I don’t care about the numerical value as in it’s order – I just want to apply precedence to if find a value from select 1 don’t bother to run second query.
Without the top 1 * I get returned 3 and 6. When I do top 1 * I get 6 and same result if I do the other select first.
Help!
When you UNION these two selects the order IS NOT DEFINED you can think of it as a RANDOM order. So you should define order to get right results. For example: