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
307 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 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

622 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