Solved

How to use DB name as variable in a Query in Sql Server Stored Procedure

Posted on 2016-09-22
11
36 Views
Last Modified: 2016-11-13
I am using 2 databases in SQL Server. Based on 1 query get Master DB name from a DB table. Using that Master DB table get values . Following is the SQL I used. I stored variable of DB name and I am not sure how to use it in 2nd query. Please help.


declare @dbname varchar(20);
declare @sql varchar(1000);
select @dbname = Databasename from ParentTable Where DBaseID = 1
      go
      set @sql = "Select B.Id, B.Name from tbl A JOIN @dbname..tbl1 B on A.CID = B.CID"
      go
      execute sp_executesql @sql
0
Comment
Question by:vidhyashan
  • 4
  • 4
  • 3
11 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41811157
you can try this:
set @sql = "Select B.Id, B.Name from tbl A JOIN " + @dbname + "..tbl1 B on A.CID = B.CID"

Open in new window

0
 
LVL 1

Author Comment

by:vidhyashan
ID: 41811176
It didn't work
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41811210
what do you see if you do
select @sql

Open in new window

0
 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 41812047
Get rid of the GOs:
declare @dbname varchar(20);
declare @sql varchar(1000);
select @dbname = Databasename from ParentTable Where DBaseID = 1
set @sql = "Select B.Id, B.Name from tbl A JOIN " + @dbname + "..tbl1 B on A.CID = B.CID"
execute sp_executesql @sql 

Open in new window

1
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41812355
Vitor is right, the GO statements can't be there!
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 1

Accepted Solution

by:
vidhyashan earned 500 total points (awarded by participants)
ID: 41831893
This is the answer I found after trying multiple ways:

set @sql = 'Select B.Id, B.Name from tbl A JOIN ' + @dbname + '..tbl1 B on A.CID = B.CID'

Its single quote which solves the issue.
0
 
LVL 1

Author Comment

by:vidhyashan
ID: 41831894
Thanks for your help folks
0
 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 41831935
If you put back the GO it won't work, right?
0
 
LVL 1

Author Comment

by:vidhyashan
ID: 41838928
I added Go just for sample. The original problem is using variable of another select statement in 2nd sql. That is the subject of this issue too.

Thanks for following up
0
 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 41859853
vidhyashan, don't forget to mark one or more comments as solution to have this question closed.
Cheers
0
 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 41885193
Recommendation to close this question by accepting the above comment as solution.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.

948 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

23 Experts available now in Live!

Get 1:1 Help Now