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 2000 Forums
 SQL Server Development (2000)
 UNION WITH IF

Author  Topic 

Clages1
Yak Posting Veteran

69 Posts

Posted - 2008-10-17 : 14:27:59


hi, i would like to do

Select a,b,c from Table A
union
Select x,y,z from Table B
union
If @saldo = 'S'
Select x,y,z from Table C
else
Select x,y,z from Table D
end-if

I get error in If statement

Tks

Clages

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2008-10-17 : 14:35:14
declare @saldo varchar(10)
set @saldo='s'

If @saldo = 'S'
Select a,b,c from TableA
union
Select x,y,z from TableB
union
Select x,y,z from TableC


else

Select a,b,c from TableA
union
Select x,y,z from TableB
union
Select x,y,z from TableD
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-18 : 02:26:03
no need of if else

select *
from
(
Select a as val1,b as val2,c as val3,1 as cat from Table A
union
Select x,y,z,2 from Table B
union
Select x,y,z,3 from Table C
union
Select x,y,z,4 from Table D
)t
where (@saldo='S' and cat<> 4)
or (@saldo<>'S' and cat<> 3)


also if you're sure that A,B,C,D wont have same records in them then use union all instead of union. union takes distinct after selection so will have an impact on performance.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-10-18 : 03:05:43


Select a,b,c from Table A
union
Select x,y,z from Table B
union
Select x,y,z from Table C where @saldo = 'S'
union
Select x,y,z from Table D where @saldo <> 'S'

Madhivanan

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

- Advertisement -