?
Solved

Select from table 1 or table 2 - MS SQL 2008

Posted on 2010-01-12
4
Medium Priority
?
298 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
[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
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

764 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