Thursday, April 17, 2008

Check if a user has access to a database in Sql Server 2005

Method 1:HAS_DBACCESS returns information about whether the user has access to the specified database (BOL).

Example:

SELECT HAS_DBACCESS('Northwind');

returns

1 if the user has access to the database
0 if the user does not have access to the database
NULL if the database does not exist

Find all databases that the current user has access to

SELECT [Name] as DatabaseName from master.dbo.sysdatabases
WHERE ISNULL(HAS_DBACCESS ([Name]),0)=1
ORDER BY [Name]


Method 2:Other best alternative of the above command is you can use sp_helplogins without paramter for checking the existing user permissions and the database list


ex
1. sp_helplogins
2. sp_helplogins 'LoginName'

No comments :