?
Solved

USE Statement with variable

Posted on 2007-11-22
2
Medium Priority
?
759 Views
Last Modified: 2008-02-01
Hi,

i am trying to script the creation of a user and add it to a database like this:

declare @DbAttachName varchar(50);
SET @DbAttachName = 'test_script';
##USE @DbAttachName;##
CREATE USER [nvt] FOR LOGIN [nvt] WITH DEFAULT_SCHEMA=[dbo];
EXEC sp_addrolemember @rolename='db_owner',@membername='nvt';

the login was created succesfully, no problem there.
on the line with the ## there is an error. apparently the USE statement does not support variables.
when i try like this:
SET @SQL='USE ' + @DbAttachName;
EXEC (@SQL);
I get no error, but the database does not change...
i suppose it only changes the database in the "EXEC" statement, and not in the script itself.

How can i solve this?
The problem is, that the database is also attached in the same script (not when testing this)
so i can not fix the USE name.
0
Comment
Question by:joachimcarrein
[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
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 20333501
> apparently the USE statement does not support variables.
correct.

>when i try like this:
>SET @SQL='USE ' + @DbAttachName;
>EXEC (@SQL);
>I get no error, but the database does not change...
 actually, it is changed, but only in the scope of that dynamic sql.
so:
>i suppose it only changes the database in the "EXEC" statement, and not in the script itself.
correct



only way:

declare @DbAttachName varchar(50);
SET @DbAttachName = 'test_script'; 
declare @sql varchar(4000)
set @sql ='USE ' + @DbAttachName + '
CREATE USER [nvt] FOR LOGIN [nvt] WITH DEFAULT_SCHEMA=[dbo]
EXEC sp_addrolemember @rolename='db_owner',@membername='nvt'
'
EXEC(@sql)

Open in new window

0
 
LVL 4

Author Comment

by:joachimcarrein
ID: 20333604
that did the trick indeed. as i suspected the use is only in the scope. but i didn't think of executing those statements in the same scope :)
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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

765 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