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?
3 Answers
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';
-
7Why 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...– zam6akCommented Oct 27, 2014 at 20:21
-
15Ancient 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.– shadowfCommented Apr 11, 2016 at 17:52
-
2
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!
-
22It'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
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/
-
1In 2023 this is a correct answer. Maybe back in 2015 it wasn't. Commented Oct 3, 2023 at 11:53