?
Solved

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

Posted on 2016-09-22
11
Medium Priority
?
55 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
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

 
LVL 51

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 2000 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 51

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 51

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 51

Expert Comment

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

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

752 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