Solved

How to drop a user or schema using dynamic SQL?

Posted on 2011-03-16
4
1,165 Views
Last Modified: 2012-05-11
Hi folks,

I am actually migrating MSSQL 2000 databases to MSSQL 2008 with an automatic script that I created for the migration. I have one script that backups all the databases on the MSSQL 2000 server, and another script that restores all the databases backups to the new MSSQL 2008 server.

Everything works fine except the following portion of code :
SET @strSqlStatement = ''
SET @strSqlStatement = 'USE ' + @databaseName
PRINT @strSqlStatement
EXEC sp_executesql @strSqlStatement
	
SET @strSqlStatement = ''
SET @strSqlStatement = 'DROP SCHEMA ' + @userName
EXEC sp_executesql @strSqlStatement
	
SET @strSqlStatement = ''
SET @strSqlStatement = 'DROP USER ' + @userName
EXEC sp_executesql @strSqlStatement

Open in new window


Here is the error I get:
Msg 15151, Level 16, State 1, Line 1
Cannot drop the schema 'usrMarketing', because it does not exist or you do not have permission.
Msg 15151, Level 16, State 1, Line 1
Cannot drop the user 'usrMarketing', because it does not exist or you do not have permission.

Open in new window


It looks like the USE statement is not doing anything, because if I run the above portion of code in the SQL Server Management Studio client tool with the database dbMarketing selected from the "Available databases" list, the two DROP statements works like a charm.

What I would like to do is to DROP the schema and the orphan user dynamically. I use a cursor that loops through all the databases to RESTORE, fetching the name of the database and the user in the @databaseName and @userName variables.

Any thoughts?

Thanks you
0
Comment
Question by:SolutionsTI
  • 3
4 Comments
 
LVL 14

Expert Comment

by:Daniel_PL
ID: 35148955
Try this approach:





SET @strSqlStatement = 'DROP SCHEMA ' + @databaseName + '.' + @userName

Open in new window

0
 
LVL 14

Accepted Solution

by:
Daniel_PL earned 500 total points
ID: 35149030
Sorry my mistake, this is working for me:

SET @strSqlStatement = 'USE '+@databaseName+' DROP SCHEMA '+ @userName

Open in new window

0
 

Author Comment

by:SolutionsTI
ID: 35149348
Works like a charm.

Thank you for the quick solution!
0
 
LVL 14

Expert Comment

by:Daniel_PL
ID: 35149634
Can you mark the solution? Thanks.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this article I will describe the Copy Database Wizard 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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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