Database access in Microsoft SQL Server

Jigar Zanzarukiya 0 Reputation points
2024-07-26T11:45:23.9833333+00:00

I am using HAS_DBACCESS function to check if user has access to the database present in sys.databases. Customer is claiming that they have given access to all the database present in server but while check it using HAS_DBACCESS it is returning 0 (zero) as output. Can someone explain me which all permission HAS_DBACCESSS function check to decide if user has access on database or not? Am I missing anything about HAS_DBACCESS function?

Which permission customer need to give to the user so that this function return 1 as output?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,988 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Erland Sommarskog 112.7K Reputation points MVP
    2024-07-26T13:20:57.2566667+00:00

    To have access to a database, a server login must:

    1. Map to a user in the database.
    2. This user must have CONNECT permission.

    If you add a login to a database, you get CONNECT permission, but this permission can be revoked or denied.

    My guess is that the customer is confused when they say that they have given access.

    You can also test this way:

    USE db
    go
    EXECUTE AS USER = 'UserToTest'
    go
    REVERT
    

    This should complete without errors if the user has access.

    0 comments No comments

  2. Zahid Butt 956 Reputation points
    2024-07-26T13:31:15.6733333+00:00

    Hi,

    In this case customer needs to give 'membership in the public role' on requested database to that user.

    For further info please refer to:

    https://learn.microsoft.com/en-us/sql/t-sql/functions/has-dbaccess-transact-sql?view=sql-server-ver16

    In security->Users right click on target user->Properties->User Mapping-> check Target database & ok:

    User's image

    0 comments No comments

  3. CathyJi-MSFT 22,306 Reputation points Microsoft Vendor
    2024-07-29T03:11:29.53+00:00

    Hello @Jigar Zanzarukiya ,

    The HAS_DBACCESS function checks several conditions to determine if a user has access to a specified database. Here are the key points it considers:

    1. Database State: The database must not be offline or in a suspect state.
    2. User Mapping: The user must be mapped to a login that has access to the database.
    3. Database Mode: The database should be in multi-user mode, or if in single-user mode, the current user must be the single user. In restricted-user mode, the user must be a member of the dbcreator or sysadmin fixed server roles, or the db_owner fixed database role.

    If these conditions are met, HAS_DBACCESS returns 1, indicating the user has access. Otherwise, it returns 0 or NULL if the database name is invalid.

    Please check these conditions whether users meet?

    Below query will list all the database the current logged in user has access to:

    SELECT name, HAS_DBACCESS(name) HasAccess
    FROM sys.databases
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.