Solved

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

Posted on 2016-09-22
11
48 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 49

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 49

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 49

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 49

Expert Comment

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

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

756 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