How do I create a new user in a SQL Azure database?

后端 未结 9 1717
礼貌的吻别
礼貌的吻别 2020-12-12 13:17

I am trying to use the following template:

-- =================================================
-- Create User as DBO template for SQL Azure Database
-- ====         


        
9条回答
  •  无人及你
    2020-12-12 13:40

    Edit - Contained User (v12 and later)

    As of Sql Azure 12, databases will be created as Contained Databases which will allow users to be created directly in your database, without the need for a server login via master.

    CREATE USER [MyUser] WITH PASSWORD = 'Secret';
    ALTER ROLE [db_datareader] ADD MEMBER [MyUser];
    

    Note when connecting to the database when using a contained user that you must always specify the database in the connection string.

    Traditional Server Login - Database User (Pre v 12)

    Just to add to @Igorek's answer, you can do the following in Sql Server Management Studio:

    Create the new Login on the server
    In master (via the Available databases drop down in SSMS - this is because USE master doesn't work in Azure):

    enter image description here

    create the login:

    CREATE LOGIN username WITH password='password';
    

    Create the new User in the database
    Switch to the actual database (again via the available databases drop down, or a new connection)

    CREATE USER username FROM LOGIN username;
    

    (I've assumed that you want the user and logins to tie up as username, but change if this isn't the case.)

    Now add the user to the relevant security roles

    EXEC sp_addrolemember N'db_owner', N'username'
    GO
    

    (Obviously an app user should have less privileges than dbo.)

提交回复
热议问题