Solved

How to use variable with use statement

Posted on 2009-05-08
6
680 Views
Last Modified: 2012-05-06
Hello,

I have a script that I'm working on that uses a variable referencing the
database name (@DBName).  When I try to use this in the form of  "USE
@DBName", it doesn't work.  If I use an nvarchar datatype and create a
string (like the following);

DECLARE @SQL nvarchar(2000)
DECLARE @DBName nvarchar(200)

SET @DBName='TestDatabase'

SET @SQL = 'USE ' + @DBName
EXEC sp_sqlexec @SQL

The script runs, but the script remains focused on the master.

Is there any way to specify USE with a variable?

Thanks!
Rick
0
Comment
Question by:RIVAGHI
6 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 100 total points
ID: 24341797
the dynamic sql has a different scope, and the scope come back to the current db; so whatever you need to accomplish to be done on  the other db, you should put those script inside that @sql variable and run it
0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 120 total points
ID: 24341927
you can't change focus but yes, you can do your task as Aneesh said, have a look at following script.
 though you are in MASTER database but can get results of Adventureworks database.


USE MASTER
go
 
DECLARE @SQL nvarchar(2000)
DECLARE @DBName nvarchar(200)
 
SET @DBName='ADVENTUREWORK'
 
SET @SQL = 'USE ' + @DBName + ';'
SET @SQL = 'SELECT * FROM SYS.OBJECTS'
EXEC sp_sqlexec @SQL

Open in new window

0
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 120 total points
ID: 24342058
In Ritesh code, you can try like this. Slightly modified his code for your requirement.

USE MASTER
go
 
DECLARE @SQL nvarchar(2000)
DECLARE @DBName nvarchar(200)
 
SET @DBName='AdventureWorks'
SET @SQL = 'USE ' + @DBName + ' SELECT * FROM SYS.Objects'
EXEC sp_sqlexec @SQL
 
You can also try with three part name.

USE MASTER
go
 
DECLARE @SQL nvarchar(2000)
DECLARE @DBName nvarchar(200)
 
SET @DBName='AdventureWorks'
SET @SQL = ' SELECT * FROM + @DBName + '.SYS.Objects'
EXEC sp_sqlexec @SQL
 
0
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.

 
LVL 41

Assisted Solution

by:pcelba
pcelba earned 100 total points
ID: 24343280
All the above written by Sharath is correct. If you really need to swith the database then you have to do it explicitly. Let suppose your database count is not infinite, so the only solution is:


DECLARE @DBName nvarchar(200)
SET @DBName='YourDatabase'
 
IF @DBName = 'Master'
   USE Master
ELSE IF @DBName = 'Model'
   USE Model
ELSE IF @DBName = 'YourDatabase'
   USE YourDatabase
ELSE
   PRINT "Invalid Database name"
 
-- Remember the USE statement is not allowed in stored procedures, functions, and triggers

Open in new window

0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 60 total points
ID: 24344001
You can also preface all objects with the database name and owner, that way you do not have to change databases.  This also means you would have to enable cross database ownership chaining.
0
 

Author Comment

by:RIVAGHI
ID: 24404500
Thank you all !!
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 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