Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Union Query
hi,

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


Report this thread to moderator Post Follow-up to this message
Old Post
jack-b@humlog.com
06-23-05 01:24 AM


Re: Union Query
Not 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
--



Report this thread to moderator Post Follow-up to this message
Old Post
David Portas
06-23-05 08:23 AM


Re: Union Query
Thanks - 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?


Report this thread to moderator Post Follow-up to this message
Old Post
jack-b@humlog.com
06-24-05 01:23 AM


Re: Union Query
SELECT 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
--



Report this thread to moderator Post Follow-up to this message
Old Post
David Portas
06-25-05 01:23 AM


Re: Union Query
Thanks David.


Report this thread to moderator Post Follow-up to this message
Old Post
jack-b@humlog.com
06-28-05 01:23 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 11:49 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006