SQL Server giving logins(users) db_owner access to database

后端 未结 4 979
后悔当初
后悔当初 2021-02-01 13:57

We have a test database and some test logins that we would like to give db_owner access to through a script. Usually we would have to go into logins and right click on the usern

4条回答
  •  礼貌的吻别
    2021-02-01 14:36

    You need to do two things, both running in the context of the target database (i.e., execute USE (database) first):

    1. Add that user as a login to the database: CREATE USER [LoginName] FOR LOGIN [LoginName]
    2. Add that user to the role: EXEC sp_addrolemember N'db_owner', N'LoginName'

    In general, if you have SQL Server Management Studio 2005 or higher, you can go into the UI for an operation, fill out the dialog box (in this case, assigning the user to the database & adding roles), and then click the "Script" button at the top. Instead of executing the command, it will write a script for the action to a new query window.

提交回复
热议问题