Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SQL table switching

Author  Topic 

geoffsmiths
Starting Member

7 Posts

Posted - 2010-05-31 : 09:29:39
Hi all!

I have 2 tables. I want the following managed in an SQL query:

If table_1 has 0 records, show results from table_2.

Can somebody share his/her expertise please?

My regards,

Geoff

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-31 : 10:04:34
if (select count(*) from table_1)>0
begin
select * from table_1
end
else
begin
select * from table_2
end


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

geoffsmiths
Starting Member

7 Posts

Posted - 2010-05-31 : 10:11:48
Webfred, you rule! How did you get that much knowledge?

Anyways, thanx again!
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-31 : 10:25:01
I have learned so much since I have joined this forums
The other mates here (like Tara, Peso, Kristen, Visakh, Madhi, Khtan, Gail, TG, sodeep, Charly and many others) are awesome!

This can be faster for big tables...but I have not tested.
Give it a try please
if exists(select * from table_1)
begin
select * from table_1
end
else
begin
select * from table_2
end


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

geoffsmiths
Starting Member

7 Posts

Posted - 2010-05-31 : 10:29:18
Thanx for the tips.

Your SQL worked great! It also works fine with small tables.

My regards!
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-31 : 10:30:26
welcome


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -