Solved

conditional union

Posted on 2009-07-16
4
260 Views
Last Modified: 2012-05-07
I have one select - if null results - want to union it with another select - is that possible?  To have a conditional union?
0
Comment
Question by:tbaseflug
  • 2
  • 2
4 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24870572
You dont really need a join here
 Just check @@ROWCOUNT

 SELECT Col1,Col2..FROM table1
IF @@ROWCOUNT = 0
 SELECT Val1 as Col1 , val2 as Col2 ... from someOtherTable
0
 

Author Comment

by:tbaseflug
ID: 24870616
well - I am returning it back to a asp.net page and really didn't want to have to check for another conditional datatable if possible
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 24870771
You can do all these in one step
there is actually no direct way to implement what you are intending , your other option is to add another column, to indicate from which statement the results are coming back

SELECT Col1, Col2 , 1 as Status
from Table1
UNION ALL
SELECT Col5, Col2, 2 as Status
from table2

Now you can do a check on status column at the asp side
0
 

Author Comment

by:tbaseflug
ID: 24870822
thanks - let me give it a shot
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question