I have a stored proc with code similar to this:
<SQL Query 1 Here>
If @.@.RowCount < 1
Begin
<SQL Query 2 Here>
End
Basically, I want to execute SQL Query 1 and if I don't get any rows,
then execute Query 2. What happens here is that I get two result sets
returned if the first one is empty. I changed my query to this:
Declare @.RecCount int
Select @.RecCount = <SQL Query 1 Here>
If @.RecCount > 0
Begin
<SQL Query 1 Here>
End
Else
Begin
<SQL Query 2 Here>
End
This way, I only get one result set, but I am wondering about
duplicating Query 1 and if that incurs a performance penalty. If so,
how can I get rid of it.
The queries themselves are simple queries that query one table with no
joins or anything else in them.
Any thoughts on this approach? Is there a 'more elegant' way of
accomplishing what I want?
Thanks,
ChrisI would do it like this
If exists (select * from table1 where ...)
Begin
select * from table1 where ...
End
Else
Begin
select * from table2 where ...
End
http://sqlservercode.blogspot.com/|||> What happens here is that I get two result sets
> returned if the first one is empty.
So? Isn't the client smart enough to see that resultset 1 is empty, and
move to the next one? In ADO, you would check for recordset.eof.
Another idea would be to do a UNION, if the resultsets are similar. If the
resultsets are not similar, then the client is going to have to perform some
logic based on which query was successful, no?
A