Hello there,

Im trying to write a query where if a query is run on a set of tables and the result of that query is null then i want it to run a different query based on different tables.

i.e.

SELECT CASE WHEN (Resulting Value from this query is NULL) THEN (Run nested query, SELECT * FROM xxx WHERE xxx)

FROM xx

WHERE xx

Any ideas would be gratefully appreciated!

Thanks

Hello,

Based on what you are trying to do I would suggest that you run a query first to determine which query you need to run using COUNT. Something like:

declare @test1 Integer
select @test1 = count(id) from firsttable where (what ever condition)
if @test1 > 1 and @test1 is not null
begin
....
Do first query
...
end
else 
...
do second query
....
end if

Hope that helps.

select IFNULL(
  (select cls_id from classes where cls_id = 6), 
  (select cls_id from classes where cls_id = 1)
)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.