?
Solved

Select from table 1 or table 2 - MS SQL 2008

Posted on 2010-01-12
4
Medium Priority
?
308 Views
Last Modified: 2012-05-08
Hello Experts,

I need to run a query that selects data from 1 table or a second table, something like -

SELECT JBAID
FROM dbo.JBAdvert OR dbo.JBExpiredAdvert
WHERE JBAID = 405 AND JBAEmployeeID = 30


or If Exists then
select from dbo.JBAdvert
Else
Select from dbo.JBExpiredAdvert

Appreciate any thoughts as to how best to do this?

Many thanks

Gareth
0
Comment
Question by:garethtnash
4 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 668 total points
ID: 26295516
NOT sure what exactly you ARE looking FOR


SELECT * from dbo.JBAdvert WHERE JBAID = 405 AND JBAEmployeeID = 30
IF @@ROWCOUNT =0
SELECT *  from dbo.JBExpiredAdvert WHERE <>

0
 
LVL 93

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 668 total points
ID: 26295540
How about...


IF EXISTS (
      SELECT JBAID
      FROM dbo.JBAdvert
      WHERE JBAID = 405)

      SELECT JBAID
      FROM dbo.JBAdvert
      WHERE JBAID = 405

ELSE

      SELECT JBAID
      FROM dbo.JBExpiredAdvert
      WHERE JBAEmployeeID = 30
0
 
LVL 27

Assisted Solution

by:Chris Luttrell
Chris Luttrell earned 664 total points
ID: 26295542
I think you want something like this UNION

SELECT JBAID
FROM dbo.JBAdvert
WHERE JBAID = 405 AND JBAEmployeeID = 30
UNION
SELECT JBAID
FROM dbo.JBExpiredAdvert
WHERE JBAID = 405 AND JBAEmployeeID = 30
0
 

Author Closing Comment

by:garethtnash
ID: 31676241
All really good thank you
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.
Suggested Courses

580 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