SQL SERVER – List Databases Where User Has DB Access

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)

Menu
Exit mobile version