|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Scripting SQL Server Permissions
I have a scenario that I want to streamline by using a SQL Script.
First, som background: ================== We have a development environment and a production environment running ASP.NET and a MS SQL Server. The ASP.NET uses a login (MyLogin) to access the SQL Server. The Login is part of the group (MyGroup) which has access to execute the stored procedures and be a datareader. The two environments have everything the same except for the server name (TheProd and TheDev ). This way the virtual directories (MyVirtualDirectory) and database names (MyDb) are the same (TheDev/MyVirtualDirectory and TheProd/MyVirtualDirectory both access a database called MyDb located at TheDev/MyDb and TheProd/MyDb). The Problem: ================== Whenever I restore the Ms SQL Database 2000 (MyDb) the login doesn't connect to the group properly and cannot access anything that the group has permissions even though it appears to be part of the group. I have to remove the user (MyLogin) from the Database and then in Enterprise Manager go into Security and add that person back into the database (public, datareader and MyGroup). The Request: ================== How can I delete the user (MyLogin) from the database (MyDb) and then add the user to the group (MyGroup)? It is not really effective to set the permissions for each table, view and stored procedure seperately and with the restoration the Group holds the permissions just fine. GRANT and DENY seems to only work with connecting a login to the table/view/stored procedure individually and not the Group. Thanks! |
|
#2
|
|||
|
|||
|
Sounds like we have the same exact set-up (using ASP.NET) and ran across the same issue. We set up a script to run after restoring the database to remove the User and then re-create it and attach it to the Role we have set up for all of the permissions.
Code:
Use [Database1] Go -- Remove the user from the database only not the server If Exists (Select * From dbo.sysusers Where name = N'YourUserName' And uid < 16382) Exec sp_dropuser 'YourUserName' Go -- Add the user back in from this server's logins If Not Exists (Select * From dbo.sysusers Where name = N'YourUserName' And uid < 16382) Exec sp_grantdbaccess N'YourUserName', N'YourUserName' Go -- Assign the db_datareader and YourUserRole roles Exec sp_addrolemember N'db_datareader', N'YourUserName' Go Exec sp_addrolemember N'YourUserRole', N'YourUserName' Go |
![]() |
| Viewing: Dev Articles Community Forums > Databases > Microsoft SQL Server > Scripting SQL Server Permissions |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|