Solved

Unable to pass variable to USE keyword

Posted on 2006-07-14
8
981 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
8 Comments
 
LVL 142

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:
EugeneZ 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 142

Expert Comment

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

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

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…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

786 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