Solved

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

Posted on 2016-09-22
11
53 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:Shanmugam Rajagopal
[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
  • 4
  • 4
  • 3
11 Comments
 
LVL 70

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:Shanmugam Rajagopal
ID: 41811176
It didn't work
0
 
LVL 70

Expert Comment

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

Open in new window

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 50

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 70

Expert Comment

by:Éric Moreau
ID: 41812355
Vitor is right, the GO statements can't be there!
0
 
LVL 1

Accepted Solution

by:
Shanmugam Rajagopal 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:Shanmugam Rajagopal
ID: 41831894
Thanks for your help folks
0
 
LVL 50

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:Shanmugam Rajagopal
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 50

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 50

Expert Comment

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

705 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