Skip to content

Latest commit

 

History

History
231 lines (195 loc) · 14 KB

sys-database-permissions-transact-sql.md

File metadata and controls

231 lines (195 loc) · 14 KB
titledescriptionauthorms.authorms.datems.servicems.subservicems.topicms.customf1_keywordshelpviewer_keywordsdev_langsmonikerRange
sys.database_permissions (Transact-SQL)
sys.database_permissions returns a row for every permission or column-exception permission in the database.
VanMSFT
vanto
06/16/2023
sql
system-objects
reference
ignite-2024
database_permissions
sys.database_permissions_TSQL
database_permissions_TSQL
sys.database_permissions
sys.database_permissions catalog view
TSQL
>=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric

sys.database_permissions (Transact-SQL)

[!INCLUDE sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw-fabricsqldb]

Returns a row for every permission or column-exception permission in the database. For columns, there is a row for every permission that is different from the corresponding object-level permission. If the column permission is the same as the corresponding object permission, there is no row for it and the permission applied is that of the object.

Important

Column-level permissions override object-level permissions on the same entity.

Column nameData typeDescription
classtinyintIdentifies class on which permission exists. For more information, see sys.securable_classes (Transact-SQL).

0 = Database
1 = Object or Column
3 = Schema
4 = Database Principal
5 = Assembly - Applies to: [!INCLUDEsql2008-md] and later versions.
6 = Type
10 = XML Schema Collection -
Applies to: [!INCLUDEsql2008-md] and later versions.
15 = Message Type - Applies to: [!INCLUDEsql2008-md] and later versions.
16 = Service Contract - Applies to: [!INCLUDEsql2008-md] and later versions.
17 = Service - Applies to: [!INCLUDEsql2008-md] and later versions.
18 = Remote Service Binding - Applies to: [!INCLUDEsql2008-md] and later versions.
19 = Route - Applies to: [!INCLUDEsql2008-md] and later versions.
23 =Full-Text Catalog - Applies to: [!INCLUDEsql2008-md] and later versions.
24 = Symmetric Key - Applies to: [!INCLUDEsql2008-md] and later versions.
25 = Certificate - Applies to: [!INCLUDEsql2008-md] and later versions.
26 = Asymmetric Key - Applies to: [!INCLUDEsql2008-md] and later versions.
29 = Fulltext Stoplist - Applies to: [!INCLUDEsql2008-md] and later versions.
31 = Search Property List - Applies to: [!INCLUDEsql2008-md] and later versions.
32 = Database Scoped Credential - Applies to: [!INCLUDEsssql16-md] and later versions.
34 = External Language - Applies to: [!INCLUDEsssql19-md] and later versions.
class_descnvarchar(60)Description of class on which permission exists.

DATABASE

OBJECT_OR_COLUMN

SCHEMA

DATABASE_PRINCIPAL

ASSEMBLY

TYPE

XML_SCHEMA_COLLECTION

MESSAGE_TYPE

SERVICE_CONTRACT

SERVICE

REMOTE_SERVICE_BINDING

ROUTE

FULLTEXT_CATALOG

SYMMETRIC_KEYS

CERTIFICATE

ASYMMETRIC_KEY

FULLTEXT STOPLIST

SEARCH PROPERTY LIST

DATABASE SCOPED CREDENTIAL

EXTERNAL LANGUAGE
major_idintID of thing on which permission exists, interpreted according to class. Usually, the major_id simply the kind of ID that applies to what the class represents.

0 = The database itself

>0 = Object-IDs for user objects

<0 = Object-IDs for system objects
minor_idintSecondary-ID of thing on which permission exists, interpreted according to class. Often, the minor_id is zero, because there is no subcategory available for the class of object. Otherwise, it is the Column-ID of a table.
grantee_principal_idintDatabase principal ID to which the permissions are granted.
grantor_principal_idintDatabase principal ID of the grantor of these permissions.
typechar(4)Database permission type. For a list of permission types, see the next table.
permission_namenvarchar(128)Permission name.
statechar(1)Permission state:

D = Deny

R = Revoke

G = Grant

W = Grant With Grant Option
state_descnvarchar(60)Description of permission state:

DENY

REVOKE

GRANT

GRANT_WITH_GRANT_OPTION

Database Permissions

The following types of permissions are possible.

Permission typePermission nameApplies to securable
AADSALTER ANY DATABASE EVENT SESSIONDATABASE
AAMKALTER ANY MASKDATABASE
AEDSALTER ANY EXTERNAL DATA SOURCEDATABASE
AEFFALTER ANY EXTERNAL FILE FORMATDATABASE
ALALTERAPPLICATION ROLE, ASSEMBLY, ASYMMETRIC KEY, CERTIFICATE, CONTRACT, DATABASE, FULLTEXT CATALOG, MESSAGE TYPE, OBJECT, REMOTE SERVICE BINDING, ROLE, ROUTE, SCHEMA, SERVICE, SYMMETRIC KEY, USER, XML SCHEMA COLLECTION
ALAKALTER ANY ASYMMETRIC KEYDATABASE
ALARALTER ANY APPLICATION ROLEDATABASE
ALASALTER ANY ASSEMBLYDATABASE
ALCFALTER ANY CERTIFICATEDATABASE
ALDSALTER ANY DATASPACEDATABASE
ALEDALTER ANY DATABASE EVENT NOTIFICATIONDATABASE
ALFTALTER ANY FULLTEXT CATALOGDATABASE
ALMTALTER ANY MESSAGE TYPEDATABASE
ALRLALTER ANY ROLEDATABASE
ALRTALTER ANY ROUTEDATABASE
ALSBALTER ANY REMOTE SERVICE BINDINGDATABASE
ALSCALTER ANY CONTRACTDATABASE
ALSKALTER ANY SYMMETRIC KEYDATABASE
ALSMALTER ANY SCHEMADATABASE
ALSVALTER ANY SERVICEDATABASE
ALTGALTER ANY DATABASE DDL TRIGGERDATABASE
ALUSALTER ANY USERDATABASE
AUTHAUTHENTICATEDATABASE
BADBBACKUP DATABASEDATABASE
BALOBACKUP LOGDATABASE
CLCONTROLAPPLICATION ROLE, ASSEMBLY, ASYMMETRIC KEY, CERTIFICATE, CONTRACT, DATABASE, FULLTEXT CATALOG, MESSAGE TYPE, OBJECT, REMOTE SERVICE BINDING, ROLE, ROUTE, SCHEMA, SERVICE, SYMMETRIC KEY, TYPE, USER, XML SCHEMA COLLECTION
COCONNECTDATABASE
CORPCONNECT REPLICATIONDATABASE
CPCHECKPOINTDATABASE
CRAGCREATE AGGREGATEDATABASE
CRAKCREATE ASYMMETRIC KEYDATABASE
CRASCREATE ASSEMBLYDATABASE
CRCFCREATE CERTIFICATEDATABASE
CRDBCREATE DATABASEDATABASE
CRDFCREATE DEFAULTDATABASE
CREDCREATE DATABASE DDL EVENT NOTIFICATIONDATABASE
CRFNCREATE FUNCTIONDATABASE
CRFTCREATE FULLTEXT CATALOGDATABASE
CRMTCREATE MESSAGE TYPEDATABASE
CRPRCREATE PROCEDUREDATABASE
CRQUCREATE QUEUEDATABASE
CRRLCREATE ROLEDATABASE
CRRTCREATE ROUTEDATABASE
CRRUCREATE RULEDATABASE
CRSBCREATE REMOTE SERVICE BINDINGDATABASE
CRSCCREATE CONTRACTDATABASE
CRSKCREATE SYMMETRIC KEYDATABASE
CRSMCREATE SCHEMADATABASE
CRSNCREATE SYNONYMDATABASE
CRSOApplies to: [!INCLUDEssSQL11] and later versions.

CREATE SEQUENCE
DATABASE
CRSVCREATE SERVICEDATABASE
CRTBCREATE TABLEDATABASE
CRTYCREATE TYPEDATABASE
CRVWCREATE VIEWDATABASE
CRXSApplies to: [!INCLUDEsql2008-md] and later versions.

CREATE XML SCHEMA COLLECTION
DATABASE
DABOADMINISTER DATABASE BULK OPERATIONSDATABASE
DLDELETEDATABASE, OBJECT, SCHEMA
EAESEXECUTE ANY EXTERNAL SCRIPTDATABASE
EXEXECUTEASSEMBLY, DATABASE, OBJECT, SCHEMA, TYPE, XML SCHEMA COLLECTION
IMIMPERSONATEUSER
ININSERTDATABASE, OBJECT, SCHEMA
RCRECEIVEOBJECT
RFREFERENCESASSEMBLY, ASYMMETRIC KEY, CERTIFICATE, CONTRACT, DATABASE, FULLTEXT CATALOG, MESSAGE TYPE, OBJECT, SCHEMA, SYMMETRIC KEY, TYPE, XML SCHEMA COLLECTION
SLSELECTDATABASE, OBJECT, SCHEMA
SNSENDSERVICE
SPLNSHOWPLANDATABASE
SUQNSUBSCRIBE QUERY NOTIFICATIONSDATABASE
TOTAKE OWNERSHIPASSEMBLY, ASYMMETRIC KEY, CERTIFICATE, CONTRACT, DATABASE, FULLTEXT CATALOG, MESSAGE TYPE, OBJECT, REMOTE SERVICE BINDING, ROLE, ROUTE, SCHEMA, SERVICE, SYMMETRIC KEY, TYPE, XML SCHEMA COLLECTION
UPUPDATEDATABASE, OBJECT, SCHEMA
VWVIEW DEFINITIONAPPLICATION ROLE, ASSEMBLY, ASYMMETRIC KEY, CERTIFICATE, CONTRACT, DATABASE, FULLTEXT CATALOG, MESSAGE TYPE, OBJECT, REMOTE SERVICE BINDING, ROLE, ROUTE, SCHEMA, SERVICE, SYMMETRIC KEY, TYPE, USER, XML SCHEMA COLLECTION
VWCKVIEW ANY COLUMN ENCRYPTION KEY DEFINITIONDATABASE
VWCMVIEW ANY COLUMN MASTER KEY DEFINITIONDATABASE
VWCTVIEW CHANGE TRACKINGTABLE, SCHEMA
VWDSVIEW DATABASE STATEDATABASE

REVOKE and column-exception permissions

In most cases, the REVOKE command will remove the GRANT or DENY entry from sys.database_permissions.

However, it is possible to GRANT or DENY permissions on a object and then REVOKE that permission on a column. This column-exception permission will show up as REVOKE in sys.database_permissions. Consider the following example:

GRANTSELECTONPerson.Person TO [Sales]; REVOKESELECTONPerson.Person(AdditionalContactInfo) FROM [Sales];

These permissions will show up in sys.database_permissions as one GRANT (on the table) and one REVOKE (on the column).

Important

REVOKE is different from DENY, as the Sales principal may still have access to the column through other permissions. Had we denied permissions rather than revoking them, Sales would not be able to view the contents of the column because DENY always supersedes GRANT.

Permissions

Any user can see their own permissions. To see permissions for other users, requires VIEW DEFINITION, ALTER ANY USER, or any permission on a user. To see user-defined roles, requires ALTER ANY ROLE, or membership in the role (such as public).

[!INCLUDEssCatViewPerm] For more information, see Metadata Visibility Configuration.

Examples

A. List all the permissions of database principals

The following query lists the permissions explicitly granted or denied to database principals.

Important

The permissions of fixed database roles do not appear in sys.database_permissions. Therefore, database principals may have additional permissions not listed here.

SELECTpr.principal_id ,pr.name ,pr.type_desc ,pr.authentication_type_desc ,pe.state_desc ,pe.permission_nameFROMsys.database_principalsAS pr INNER JOINsys.database_permissionsAS pe ONpe.grantee_principal_id=pr.principal_id; 

B. List permissions on schema objects within a database

The following query joins sys.database_principals and sys.database_permissions to sys.objects and sys.schemas to list permissions granted or denied to specific schema objects.

SELECTpr.principal_id ,pr.name ,pr.type_desc ,pr.authentication_type_desc ,pe.state_desc ,pe.permission_name ,s.name+'.'+o.nameAS ObjectName FROMsys.database_principalsAS pr INNER JOINsys.database_permissionsAS pe ONpe.grantee_principal_id=pr.principal_idINNER JOINsys.objectsAS o ONpe.major_id=o.object_idINNER JOINsys.schemasAS s ONo.schema_id=s.schema_id;

C. List permissions for a specific object

You can use the previous example to query permissions specific to a single database object.

For example, consider the following granular permissions granted to a database user test in the sample database [!INCLUDE sssampledbdwobject-md]:

GRANTSELECTONdbo.vAssocSeqOrders TO [test];

Find the granular permissions assigned to dbo.vAssocSeqOrders:

SELECTpr.principal_id ,pr.name ,pr.type_desc ,pr.authentication_type_desc ,pe.state_desc ,pe.permission_name ,s.name+'.'+o.nameAS ObjectName FROMsys.database_principalsAS pr INNER JOINsys.database_permissionsAS pe ONpe.grantee_principal_id=pr.principal_idINNER JOINsys.objectsAS o ONpe.major_id=o.object_idINNER JOINsys.schemasAS s ONo.schema_id=s.schema_idWHEREo.name='vAssocSeqOrders'ANDs.name='dbo';

Returns the output:

principal_id name type_desc authentication_type_desc state_desc permission_name ObjectName 5 test SQL_USER INSTANCE GRANT SELECT dbo.vAssocSeqOrders 

See also

Next steps

close