Solved

How do I write a series of SQL Server SELECT statements that compare tables in two databases with variables for the db names?

Posted on 2014-12-30
3
298 Views
Last Modified: 2014-12-30
I want to write something like this

declare @db1 nvarchar(100);
declare @db2 nvarchar(100);

select id from [@db1]..MyTable t1 INNER JOIN [@db2]..MyTable t2 on [t1].id=[t2].id

but clearly SQL Server doesn't like the database names in variables - how can I achieve what I am trying to do?

I'm on but I'd like it as generic as possible for versions:
Microsoft SQL Server 2012 - 11.0.5058.0 (X64)
      May 14 2014 18:34:29
      Copyright (c) Microsoft Corporation
      Standard Edition (64-bit) on Windows NT 6.3 <X64> (Build 9600: )
0
Comment
Question by:purplesoup
3 Comments
 
LVL 15

Accepted Solution

by:
Vikas Garg earned 500 total points
ID: 40523450
Hi,

You can do something like this
declare @db1 nvarchar(100) = 'KPIFStaging'
declare @db2 nvarchar(100) = 'WintimeStaging'
DECLARE @SQL NVARCHAR(500)


SET @SQL = 'SELECT * FROM ' + @db1 + '.[dbo].[TransactionAudit]'

--PRINT @SQL -- To check the SQL

EXECUTE(@SQL)

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40523459
I concur you cannot do without going dynamic sql  ( or going ad-hoc sql from outside the sql as such)
you cannot use variables for any of the following : database, table, column names etc ...
0
 

Author Comment

by:purplesoup
ID: 40523492
Ok thanks - when I saw the answer I guessed there wasn't going to be some more useful way to do it. I'm supplying a script to someone and it would have been useful for them to just set the names of the databases at the top of the script, but I think I'm just going to tell them to do a find and replace on the database names, going the dynamic sql route is going to ruin readability.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

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…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

860 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