Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to use variable with use statement

Posted on 2009-05-08
6
Medium Priority
?
718 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
[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
6 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 400 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 480 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 41

Assisted Solution

by:Sharath
Sharath earned 480 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 43

Assisted Solution

by:pcelba
pcelba earned 400 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 240 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

609 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