I want to grant access to a database for a SQL Server login. I know about sp_grantdbaccess, however it is deprecated. What can I use instead and how can I check if the login does not already have access to the database?
Scenario: UserA creates database - in sys.database_principals I have an entry with name dbo and with the sid of UserA. I try to grant UserA permisions again: I try to do a select on sys.database_principals by name='UserA', however since name is dbo not UserA, I get an error - 'The login already has an account under a different user name.'. How can I determine if the user has database access without the sid?