Creating a user, mapping to a database and assigning roles to that database using SQL scripts

后端 未结 2 1662
情深已故
情深已故 2021-01-13 12:48

I\'ve done this before but not through scripts. I\'ve to create a new user in SQL server (SQL Authentication) and map the user to a database and assign the roles for the use

2条回答
  •  臣服心动
    2021-01-13 13:31

    The above solution works for me. However if the Username doesnt yet exist in that database as a USER, that username will not be fully mapped to the database role.

    In this situation I first add the user:

    USE databasename
    
    CREATE USER [DOMAIN\svce_name] FOR LOGIN [DOMAIN\svce_name] WITH DEFAULT_SCHEMA=[dbo]
    GO
    

    Then I add the role:

    USE databasename
    EXEC sp_addrolemember N'db_ssisoperator', N'DOMAIN\svce_name'
    GO
    

提交回复
热议问题