SQL SERVER – List Databases Where User Has DB Access

SQL SERVER - List Databases Where User Has DB Access HasDBAccess-800x1073 Recently I got an email where my client asked if I know a way to know to which database the user has access. Well, it is pretty straightforward to know with the help of function HAS_DBACCESS. This is a very interesting function and it helps us to list databases where User Has DB Access. Let us see the example.

Here is the script which you can run to know to which database you have access. Now the function HAS_DBACCESS returns 1 if the user has access to the database, otherwise, it will return as 0. If you see value NULL, that means the database name passed to the function is invalid.

Has DB Access

SELECT name AS DatabaseName,
HAS_DBACCESS(name) AS HasDBAccess
FROM sys.databases
WHERE HAS_DBACCESS(name) = 1

Here are my few recent videos and I would like to know what is your feedback about them. Do not forget to subscribe SQL in Sixty Seconds series. I hope you appreciate learning about the Last 5 SQL in Sixty Seconds Video.

If you have any questions, you can always reach out to me on Twitter.

Reference: Pinal Dave (http://blog.SQLAuthority.com)

SQL Scripts, SQL Server, SQL Server Security
Previous Post
MySQL – Wait For Seconds Using SELECT SLEEP()
Next Post
SQL SERVER – Script to Estimate Compression

Related Posts

3 Comments. Leave new

  • Even if I create a new database not create a user, the HAS_DBACCESS returns 1. Some default users are created with default db creation.

    Reply
  • This is exactly what I needed – thank you!

    Reply
  • Lars Panzerbjrn
    February 26, 2024 3:52 pm

    I don’t really know SQL well, so I don’t get where I specify which user it is checking for?

    Reply

Leave a Reply