Solved

How to drop a user or schema using dynamic SQL?

Posted on 2011-03-16
4
1,149 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
Comment Utility
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
Comment Utility
Sorry my mistake, this is working for me:

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

Open in new window

0
 

Author Comment

by:SolutionsTI
Comment Utility
Works like a charm.

Thank you for the quick solution!
0
 
LVL 14

Expert Comment

by:Daniel_PL
Comment Utility
Can you mark the solution? Thanks.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now