Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if user is member of dbo role in SQL Server

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...

like image 262
Mattias Lindberg Avatar asked Oct 30 '12 14:10

Mattias Lindberg


People also ask

How do you check what roles are assigned to a user in SQL?

To find all the role assignments to users in SQL Server database, you can use the following query. SELECT r.name role_principal_name, m.name AS member_principal_name FROM sys. database_role_members rm JOIN sys. database_principals r ON rm.

What is DBO user in SQL Server?

The dbo user is a special user principal in each database. All SQL Server administrators, members of the sysadmin fixed server role, sa login, and owners of the database, enter databases as the dbo user. The dbo user has all permissions in the database and cannot be limited or dropped.

Is SQL a role member?

To determine whether the current user is a member of the specified Windows group or SQL Server database role, use IS_MEMBER (Transact-SQL). To determine whether a SQL Server login is a member of a server role, use IS_SRVROLEMEMBER (Transact-SQL).


1 Answers

IS_ROLEMEMBER?

IF IS_ROLEMEMBER ('db_owner') = 1
BEGIN
    PRINT 'Is owner'
END

Or, if querying for a different user:

IF IS_ROLEMEMBER ('db_owner','other user') = 1
BEGIN
    PRINT 'Is owner'
END
like image 152
Damien_The_Unbeliever Avatar answered Oct 06 '22 02:10

Damien_The_Unbeliever