Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to drop a user or schema using dynamic SQL?

Posted on 2011-03-16
4
Medium Priority
?
1,185 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .

927 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