SQL Server giving logins(users) db_owner access to database

后端 未结 4 987
后悔当初
后悔当初 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:41

    I'd like to propose another solution which may help someone...

    -- create the user on the master database
    USE [master] 
    GO
    CREATE LOGIN [MyUserName] WITH PASSWORD=N'MyPassword'
    CREATE USER [MyUserName] FOR LOGIN [MyUserName]
    GO
    
    -- create the user on the target database for the login
    USE [MyDatabaseName]
    GO
    CREATE USER [MyUserName] FOR LOGIN [MyUserName]
    GO
    
    -- add the user to the desired role
    USE [MyDatabaseName]
    GO
    ALTER ROLE [db_owner] ADD MEMBER [MyUserName]
    GO
    

提交回复
热议问题