Click here to Skip to main content
15,915,019 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,

I need to make a select statement that if query 1 return no results , so it should execute another query

for example :
select username from users where id = (select top 1 id from table1)

i want if query1 (select top 1 id from table1) return null or 0 results ,
so it should execute query 2 =(select top 1 id from table2)

IS that possible ?

note : i don't want to use any server side code like php or c#
i need to do it only using SQL
Posted

You can use @@ROWCOUNT[^] for that...
SQL
SELECT ... FROM ...

IF(@@ROWCOUNT = 0)
  SELECT ... FROM ...
 
Share this answer
 
Try NOT EXISTS[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900