Solved

Conditionally accesing a possibly non existent table from other DB

Posted on 2011-09-28
2
240 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
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL: launch actions one before the other 10 24
Help Required 2 39
SQL - Simple Pivot query 8 15
SQL - Curser to do an insert based on a select 2 14
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

820 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