Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Drop User and Create User on db

$
0
0

Hi,

Currently we have db's that are versions of our intranet db's on a different server.  We have been experiencing problems with the main User on those duplicate databases after an backup and restore of the db's.  The user exists on the db and the user mappings are correct for that user but the user cannot access the db and as a result the application fails.  The fix is to go into the db, drop the user, then add the user back in with the correct roles in the User Mappings.  This has been happening sporadically since SQL 2005.

We are still sometimes having this issue in SQL2008 and have implemented a server job to address the issue should it happen.  The server job runs the following script in a step for each db.

ECHO On
SET destination_server=myServerName
SET db_name=myDBName
sqlcmd -S %destination_server% -V10 -m1 -Q "USE %db_name%; DROP USER [myUserName]; CREATE USER [myUserName]; EXEC sp_addrolemember N'db_owner', N'myUserName';"

The type is an 'Operating system [CmdExec]' and is run as 'SQL Server Agent Service Account'.

This code executes without issue, but as a test I took away all the roles for the user, then executed the script and even though it executed successfully, the user was not dropped and the role of db_owner was not reapplied.  I can only assume from that, the script failed.

Can someone shed some light on this for me?

thanks


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>