Suppose two tables test1 and test2 on which I select values from both tables using a join.
My requirement is this: when I join the two tables, if test1 has data in it but no corresponding data is found in test2, then the query should throw exception.
How can this be accomplished?
You need RAISEERROR
Do left join and select where the right side is null. If the count > 0 throw the exception.
if you need to return the results where don’t match, then add the following ELSE
You could always, put the results into a temp table and count off that, and then return off that too if you prefer to do the query only once.