I need a T-SQL statement to check if a user is member of a database role in SQL Server. Specifically I need to know if the user is member of the dbo role, because then I don’t have to grant additional authority to that user.
If I try to add additional authority when the user is dbo it fails, and my script fails…
This is what I ended up doing: (edit based on comment)
The $(ContentAccount) is of course a parametrization which has the user domain and name!
This solves my problem because when we deploy a new database we are assigning permissions manually. But in development environments where the user we try to add already is sysadmin they fail. So if we check for sysadmin membership that is enough to cover the dev server scenario.
Then I do this to check membership: