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 based on a condition

Author  Topic 

Babli
Yak Posting Veteran

53 Posts

Posted - 2007-02-08 : 01:44:19
Hi,

I have an input parameter when its true there should be a union.

My stored proc should look something:

SELECT * FROM TABLE1

if @param is true then

SELECT * FROM TABLE1
UNION
SELECT * FROM TABLE2

ELSE

SELECT * FROM TABLE1.

How do i do this.

Help needed.

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-02-08 : 01:51:23
[code]If @param = 1
Begin
Select * from Table1
Union all
Select * from Table2
End
Else
Begin
Select * from Table1
End
[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-02-08 : 02:14:05
[code]
SELECT * FROM TABLE1
UNION
SELECT * FROM TABLE2
WHERE @param = 1
[/code]


KH

Go to Top of Page

Babli
Yak Posting Veteran

53 Posts

Posted - 2007-02-08 : 02:14:28
Thanks a ton

quote:
Originally posted by harsh_athalye

If @param = 1
Begin
Select * from Table1
Union all
Select * from Table2
End
Else
Begin
Select * from Table1
End


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"

Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-02-08 : 02:18:58
quote:
Originally posted by khtan


SELECT * FROM TABLE1
UNION
SELECT * FROM TABLE2
WHERE @param = 1



KH





Will not that be slightly more costly?

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-02-08 : 02:41:39
Yes. slightly


KH

Go to Top of Page
   

- Advertisement -