13

I have the following script:

ALTER ROLE [db_datareader] ADD MEMBER [DomainGroup123]

when I run this against SQL Server 2008 R2 I get this error:

Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'ADD'.

I have looked online, and found examples that use this exact statement (but with a different user.)

I have double checked that the login exists and is a valid user on the database I am using. Also I have SA permissions on the server.

What am I mssing?

2
  • 7
    Although you've already found a solution, please note that the documentation for ALTER ROLE shows that the syntax you tried is only valid in SQL Server 2012, not SQL Server 2008 R2. Commented Feb 26, 2013 at 13:19
  • @Pondlife is right. Note: You should use "ALTER ROLE" for any future development in SS2012 on up. Proof: Microsoft's "Important" Note here on sp_addrolemember explicitly says to use "ALTER ROLE" instead. Commented Dec 8, 2014 at 19:36

3 Answers 3

24

Use sp_addrolemember.

EXECUTE sp_addrolemember db_datareader, 'UserName'
Sign up to request clarification or add additional context in comments.

Comments

9

Found this answer: https://stackoverflow.com/a/456365/16241

That showed me that I can run it like this:

exec sp_addrolemember db_datareader, [DomainGroup123]

I did that and it worked.

Comments

4

I've noticed that depending on the version of the server, I need to go one of the following.

alter role RoleName add member UserName

or

execute sp_addrolemember RoleName, UserName

I'm thinking of changing my approach from trial-and-error to some kind of conditional but (a) this operation is performed quite seldom in my case and (b) I'm a bit lazy nowadays.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.