Solved

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

Posted on 2016-09-22
11
30 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 45

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 45

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 45

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 45

Expert Comment

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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

743 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

15 Experts available now in Live!

Get 1:1 Help Now