What’s the syntax for creating a stored procedure that contains two queries, if the first query returns a set of records then don’t run the second. I’m guessing there is going to have to be some test on the count so that if the first query returns a set the second won’t run.
I would like to do this:
- Run query
- Test result set from first query
- If count equal to zero then run second query
I always like adding is there a ‘correct’ way to do this or in fact ‘should’ this type of stored procedure be constructed or is it generally frowned upon?
Thanks
You can use @@ROWCOUNT to check and see the number of rows affected with the previous query.
As to your question about is this something you should be doing, I think that would be case by case. It is similar to putting control flow logic inside any other program, sometimes
IFstatements are the correct tool to achieve your goals, but they should not be used in every situation.