T-SQL How to grant role to user

Sql ServerSql Server-2008Tsql

Sql Server Problem Overview


After creating roles and granting privileges to them, I want to grant the privileges of a specified role to a user, how to do with it? I tried grant to , but failed.

Sql Server Solutions


Solution 1 - Sql Server

Because BOL shows sp_addrolemember has been deprecated, if you are using SQL Server 2012 you may want to use:

ALTER ROLE <role_name> ADD MEMBER <user_name>

Solution 2 - Sql Server

EXEC sp_addrolemember 'db_owner', 'JohnJacobs'

Solution 3 - Sql Server

In my case, I needed to add the user to the public role for a specific database. This is done as so:

USE [<DB Name>]
GO
CREATE USER [<User Name>] FOR LOGIN [<User Name>]
GO

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontmjView Question on Stackoverflow
Solution 1 - Sql ServersampView Answer on Stackoverflow
Solution 2 - Sql ServerRaabView Answer on Stackoverflow
Solution 3 - Sql ServerSlogmeister ExtraordinaireView Answer on Stackoverflow