Solved

Unable to pass variable to USE keyword

Posted on 2006-07-14
8
982 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 42

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

730 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