Solved

Unable to pass variable to USE keyword

Posted on 2006-07-14
8
984 Views
Last Modified: 2008-02-26
Hi,

I am not sure why we cannot pass variables to USE keyword, e.g. USE @dbname.  I have tried using EXEC sp_executesql @dbexec, where @dbname contains the value 'USE ' + @dbname, however, it will not really switch to the new database.

E.g.
declare @dbname nvarchar(30)
declare @dbexec nvarchar(30)

set @dbname = 'new_test_db'
set @dbexec = 'USE ' + @dbname

exec sp_executesql @dbexec

Now that it is executed, it will of course change back to Master (this also the case if we use EXEC (@dbname).  I would like it to remain in the new database that I have chosen, e.g. new_test_db so that I can manipulate more data inside there as per the USE keyword.

How can I really accomplish this in SQL Server 2000 Query Analyzer please.  I would like to do this as we have lots of databases that ends with a numbers, so that I can perform a loop.

I thank you in advance for your help.
0
Comment
Question by:Charmed_Empress
[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
8 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17106679
you cannot do it, sorry to say.
0
 
LVL 30

Expert Comment

by:nmcdermaid
ID: 17106711
Don't know if it helps but you can do this:

EXEC <dbname>.<schemaname>.<stored proc name>


i.e.

EXEC YourDB.dbo.p_AStoredProcedure



or this


SELECT * FROM YourDB.dbo.YourTable


In other words you can prefix the object with a database name.



.... and I beleive you can use this format in sp_executsql.




declare @dbname nvarchar(30)
declare @dbexec nvarchar(30)

set @dbname = 'TestDB'
set @dbexec = 'SELECT * FROM ' + @dbname + '.dbo.YourTable'
sp_executesql @dbexec
0
 
LVL 43

Accepted Solution

by:
Eugene Z earned 125 total points
ID: 17108498
you can use it if you put the t-sql in the same variable:
for example:

declare @dbname nvarchar(30)
declare @dbexec nvarchar(30)

set @dbname = 'new_test_db'
set @dbexec = 'USE ' + @dbname + ' Select * from sometable'

exec (@dbexec)
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 17115316
dont really know what you are trying to do but if your purpose is to select  or do some transaction
onanother database other that the currenct one then you could always use something like this


select * from [northwind].dbo.custiomers

or insert into [northwind].dbo.custiomers
values ('','','')
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17595793
My Suggestion:
  accept EugeneZ's comment
0

Featured Post

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

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

623 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