Drop Table
Support Forum for database administrators and web based access to important newsgroups related to databaseshi, Can you union 2 queries with an IF statement between them? e.g. select a, b from mtTable where a = c union if ab = x begin select a, b from mtTable where a = c end Cheers, Jack
Post Follow-up to this messageNot exactly. But you could put the extra condition in a WHERE clause. I'm not clear what AB is in your example but any expression that can go after your IF statement will be valid in the WHERE clause. SELECT a, b FROM mtTable WHERE a = c UNION SELECT a, b FROM mtTable WHERE a = c AND ab = x -- David Portas SQL Server MVP --
Post Follow-up to this messageThanks - worked perfectly! You don't know how I can keep the order in between queries? (order by) Do I have to use a sub-query for each select statement?
Post Follow-up to this messageSELECT a,b FROM (SELECT a, b, 1 AS ord FROM mtTable WHERE a = c UNION SELECT a, b, 2 FROM mtTable WHERE a = c AND ab = x) AS T ORDER BY ord -- David Portas SQL Server MVP --
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread