Solved

Conditionally accesing a possibly non existent table from other DB

Posted on 2011-09-28
2
242 Views
Last Modified: 2012-08-13

Hi, I have a SELECT in a stored procedure that access a table in a database distinct of the
database where this SELECT is, by means of a LEFT JOIN:

SELECT
    A.id, B.id
FROM
     A LEFT JOIN OTHER_DB.dbo.B ON A.Id = B.Id

the thing is, OTHER_DB may not exist, and I want to know if it is possible to use conditional
compilation (or another solution), to have a code that can be used whether that other db exists or
not (using only table A in case OTHER_DB doesn't exists); the only way I can think is simply put
the code that references that db enclosed in commentary marks like this:

SELECT
    A.id--, B.id
FROM
     A --LEFT JOIN OTHER_DB.dbo.B ON A.Id = B.Id

but I'm curious if other more seamless or practical way is possible, in order to maintain more
easily my code, I'm not sure if it possible something like that... thanks in advance.

0
Comment
Question by:GHR_GDL
[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 Comments
 
LVL 21

Accepted Solution

by:
JestersGrind earned 250 total points
ID: 36719497
Typically a left join is used to perform a conditional join.  Your situation is a little different because the table may or may not exist.  There are two ways that I can think of that might work.  You could have two versions of the query in your stored procedure, one with the table and without.  And then, use IF EXISTS(SELECT * FROM sys.databases WHERE name = OTHER_DB) query with OTHER_DB ELSE query without OTHER_DB.

The other way is to use dynamic SQL and build your select statement based on whether the database exists or not.

Greg

0
 

Author Closing Comment

by:GHR_GDL
ID: 36818746
Lead me to try other stuff that worked for me.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

627 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