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
Solved

conditional union

Posted on 2009-07-16
4
261 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Re-appearing SQL Server Agent jobs 7 29
SQL Query 2 31
MS SQL Server select from Sub Table 14 23
job schedule 8 18
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

856 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