Solved

Conditionally accesing a possibly non existent table from other DB

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now