I have a query that regularly returns "nothing", and I would like to run a different query if this is the case, but I know not of the way of doing this. If anyone could be of help please.
Here is the current code I am using...
SELECT * FROM cfg_users JOIN cfg_ash ON cfg_users.iUserId = cfg_ash.iUserid WHERE iTeamId
='0' AND sDisabled IS NULL AND iStatusId > 0 AND sDate = '2014-08-01' GROUP BY cfg_users.iUserId
ORDER BY iStatusId, sName
I basically want to say
IF <my code> IS NULL THEN <do other code>, IF <my code> IS NOT NULL THEN return the result.
Thanks
There are some simple way only use sql.
Define your first query as a temp table, with union all, filter the second query with temp table's count.
This query will return the second table's records.
And if temp query have result, only return temp query.
You can test with sql fiddle here.
You can store the results in a temporary table / table variable, and then check the count
e.g.
n.b. you should really specify what columns you want in both queries rather than using *
A way you can do it is like this
set two variables equal to the queries you want to execute.
set another variable equal to the correct query when the first is not null.
execute that query with a stored procedure.
STORED PROCEDURE:
THE TWO SELECTS YOU WANT TO EXECUTE:
THE DEFINER TO GET THE CORRECT QUERY:
EXECUTE THE STATEMENT: