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.
- Detecting Memory Pressure – SQL in Sixty Seconds #186
- CPU Running 100% – SQL in Sixty Seconds #185
- Generate Script of SQL Server Objects – SQL in Sixty Seconds #184
- Prevent Unauthorized Index Modifications – SQL in Sixty Seconds #183
- MAX Columns Ever Existed in Table – SQL in Sixty Seconds #182
- Tuning Query Cost 100% – SQL in Sixty Seconds #181
- Queries Using Specific Index – SQL in Sixty Seconds #180
- Read Only Tables – Is it Possible? – SQL in Sixty Seconds #179
- One Scan for 3 Count Sum – SQL in Sixty Seconds #178
- SUM(1) vs COUNT(1) Performance Battle – SQL in Sixty Seconds #177
- COUNT(*) and COUNT(1): Performance Battle – SQL in Sixty Seconds #176
If you have any questions, you can always reach out to me on Twitter.
Reference:Â Pinal Dave (http://blog.SQLAuthority.com)
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.
This is exactly what I needed – thank you!
I don’t really know SQL well, so I don’t get where I specify which user it is checking for?