46

I would like to create an SQL Azure user and grant her read-only access on a handful of DBs, what script can I use to achieve this?

1

3 Answers 3

100
Answer recommended by Microsoft Azure Collective

A pure TSQL script is super messy, SQL Azure disables the USE command, so you are stuck opening connections to each DB you need to give the user read access.

This is the gist of the pattern.

In Master DB:

CREATE LOGIN reader WITH password='YourPWD';
-- grant public master access
CREATE USER readerUser FROM LOGIN reader;

In each target DB (requires a separate connection)

CREATE USER readerUser FROM LOGIN reader;
EXEC sp_addrolemember 'db_datareader', 'readerUser';
3
  • 7
    Why is there a "CREATE USER..." in master DB? In all docs I looked at, it only says you have to create user in target DB...Just curious...
    – zam6ak
    Commented Oct 27, 2014 at 20:21
  • 15
    Ancient answer and old comment, but for anyone interested... I think he's included the create user in master DB so the user (readerUser) can login to the server instance via Management Studio - if the user doesn't exist under master, they won't be able to use SQL authentication in Management Studio to sign in.
    – shadowf
    Commented Apr 11, 2016 at 17:52
  • 2
    Grant permissions at object level or for selected tables check this and this , hope helps.
    – Shaiju T
    Commented Sep 27, 2016 at 9:43
4

OR... Use the Azure User Management console - AUMC to manage the Logins and Users.

It's a open source project available on codeplex AUMC.codeplex.com

Project Description

Azure User Management Console - AUMC is a User Graphic Interface (GUI) that manages the users and logins of an Azure SQL database. The tool is simply converting your action into T-SQL commands and execute them on the Azure SQL Database.

A quick simple tool with a user interface! Don

Enjoy!

1
  • 22
    It's a little spooky to plug your database credentials into apps just downloaded from the internet ... just saying ... Commented Aug 18, 2016 at 4:23
2

You can create new user without creating login on master DB (which is require make a separate connection)

CREATE USER user1 WITH password='<Strong_Password>';

https://azure.microsoft.com/en-us/documentation/articles/sql-database-manage-logins/

1
  • 1
    In 2023 this is a correct answer. Maybe back in 2015 it wasn't. Commented Oct 3, 2023 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.