Solved

conditional union

Posted on 2009-07-16
4
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CASE Statement using different fields 8 28
Sorting a SQL script 5 41
SQL - Aging Report - Display Months with no data 8 43
When are cursors useful? 8 60
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

734 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