Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
315 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
[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
3 Comments
 
LVL 15

Accepted Solution

by:
Vikas Garg earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

688 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