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
Solved

USE Statement with variable

Posted on 2007-11-22
2
740 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
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query still returning duplicates 5 41
Syntax Issue with SSIS module 26 100
Check ALL SP in database make sure there are no errors 17 43
SQL query and VBA 5 41
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

791 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