HAS_DBACCESS (Transact-SQL)
Applies to:
SQL Server
Azure SQL Managed Instance
Azure Synapse Analytics
Analytics Platform System (PDW)
Returns information about whether the user has access to the specified database.
Transact-SQL syntax conventions
Syntax
HAS_DBACCESS ( 'database_name' )
Note
To view Transact-SQL syntax for SQL Server 2014 (12.x) and earlier versions, see Previous versions documentation.
Arguments
'database_name'
The name of the database for which the user wants access information. database_name is sysname.
Return Types
int
Remarks
HAS_DBACCESS returns 1 if the user has access to the database, 0 if the user has no access to the database, and NULL if the database name is not valid.
HAS_DBACCESS returns 0 if the database is offline or suspect.
HAS_DBACCESS returns 0 if the database is in single-user mode and the database is in use by another user.
Permissions
Requires membership in the public role.
Examples
The following example tests whether current user has access to the AdventureWorks2022
database.
SELECT HAS_DBACCESS('AdventureWorks2022');
GO
Examples: Azure Synapse Analytics and Analytics Platform System (PDW)
The following example tests whether current user has access to the AdventureWorksPDW2012
database.
SELECT HAS_DBACCESS('AdventureWorksPDW2012');
GO