Hi All,
i have three Union ALL statements with three tables , In one
scenario i need only two tables, Other than case , is there any better
way???
Thanks in Advance
Thomson
John Bell - 30 Dec 2008 10:17 GMT
> Hi All,
> i have three Union ALL statements with three tables , In one
[quoted text clipped - 4 lines]
>
> Thomson
Hi
It is not clear what you are asking, posting ddl and your statements would
help.
John
amish - 30 Dec 2008 14:05 GMT
> Hi All,
> i have three Union ALL statements with three tables , In one
[quoted text clipped - 4 lines]
>
> Thomson
I dont know your scenario
Try this
create table #test(id int)
create table #test1(id int)
create table #test2(id int)
insert into #test values (1)
insert into #test1 values (2)
insert into #test2 values (3)
declare @flag bit
set @flag = 0
/* if flag is = 0 then will union first two tables else it will union
all three tables */
select * from #test
union all
select * from #test1
union all
select * from #test2 where 1 = case @flag when 0 then 0 else 1 end
Thanks and Regards
Amish Shah
http://shahamishm.blogspot.com
--CELKO-- - 30 Dec 2008 14:20 GMT
"A problem well stated is a problem half solved." -- Charles F.
Kettering
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. If you know how, follow ISO-11179 data element naming
conventions and formatting rules. Temporal data should use ISO-8601
formats. Code should be in Standard SQL as much as possible and not
local dialect.
Sample data is also a good idea, along with clear specifications. It
is very hard to debug code when you do not let us see it. If you want
to learn how to ask a question on a Newsgroup, look at:
http://www.catb.org/~esr/faqs/smart-questions.html
TheSQLGuru - 30 Dec 2008 18:38 GMT
I think better than a case is an IF:
if @idontneed3tables = 0
begin
select .. from a union all select ... from b
end
else
begin
select .. from a union all select ... from b select .. from c
end
that guarantees optimal plan regardless of anything

Signature
Kevin G. Boles
Indicium Resources, Inc.
SQL Server MVP
kgboles a earthlink dt net
> Hi All,
> i have three Union ALL statements with three tables , In one
[quoted text clipped - 4 lines]
>
> Thomson
Alex Kuznetsov - 30 Dec 2008 19:59 GMT
> Hi All,
> i have three Union ALL statements with three tables , In one
[quoted text clipped - 4 lines]
>
> Thomson
You can create two views and select from one of them depending on
something. However, are you aware of partitioning? Shouldn't you store
all similar data in one table?