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 Execute

Author  Topic 

j11SqlTeam
Starting Member

1 Post

Posted - 2009-03-02 : 22:50:49
How do I properly write the following?

IF(@ParamVar > 0)
Execute (SELECT * FROM TABLE1)
ELSE
Execute (SELECT * FROM TABLE1 UNION SELECT * FROM TABLE2)

Thank you!

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-03-02 : 23:07:51
IF(@ParamVar > 0)
Execute ('SELECT * FROM TABLE1')
ELSE
Execute ('SELECT * FROM TABLE1 UNION SELECT * FROM TABLE2')
Go to Top of Page

matty
Posting Yak Master

161 Posts

Posted - 2009-03-02 : 23:40:31
IF(@ParamVar > 0)
SELECT * FROM TABLE1
ELSE
SELECT * FROM TABLE1
UNION
SELECT * FROM TABLE2

Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-03-02 : 23:42:34
IF(@ParamVar > 0)
SELECT * FROM TABLE1
ELSE
begin
SELECT * FROM TABLE1
UNION
SELECT * FROM TABLE2
end
rememeber while using union the select columns should be same no of columns and same datatype
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-03-03 : 02:40:04

Will this work?

SELECT * FROM TABLE1
UNION
SELECT * FROM TABLE2 WHERE @ParamVar <= 0


Madhivanan

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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-03 : 09:16:56
quote:
Originally posted by madhivanan


Will this work?

SELECT * FROM TABLE1
UNION
SELECT * FROM TABLE2 WHERE @ParamVar <= 0


Madhivanan

Failing to plan is Planning to fail


yup it will
Also adding to that both tables should have same number of columns with corresponding ones of same type.
If its certain that duplicates dont exist, use UNION ALL instead of union.
Go to Top of Page
   

- Advertisement -