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
 General SQL Server Forums
 New to SQL Server Programming
 Conditional union possible?

Author  Topic 

Danny__T
Starting Member

27 Posts

Posted - 2005-12-15 : 10:45:22
I'm trying to achieve something like the following:


SELECT 'page' as type, page_filename as filename, page_title as title, page_metadescription as intro
FROM tbl_pages
WHERE CONTAINS(*, @searchterm)

IF 1=1 BEGIN
UNION all

SELECT 'news' as type, 'thiswillbeafilename.asp' as filename, news_title as title, news_intro as intro
FROM tbl_news
WHERE CONTAINS(*, @searchterm)
END
IF 2=2 BEGIN
UNION ALL

SELECT 'resource' as type, resource_filename as filename, resource_longtitle as title, resource_summary
FROM tbl_resources
WHERE CONTAINS(*, @searchterm)
END


I'm pretty sure this isn't possible using the approach i have here, however is there an different method i can use to ahcieve the same effect other than using dynamic sql?

Cheers,

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-12-15 : 11:14:34
how about:

select...
from...
where...

union all

select...
from...
where...
and 1 = case when <true condition> then 1 else 2 end


Be One with the Optimizer
TG
Go to Top of Page

Danny__T
Starting Member

27 Posts

Posted - 2005-12-15 : 11:30:10
Thank youuuuuuuuuuuuuuu!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-12-15 : 23:49:14
Why are you checking for 1=1 and 2=2?
They are always true

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -