Adds a security account in the current database for a Microsoft® SQL Server™ login or Microsoft Windows NT® user or group, and enables it to be granted permissions to perform activities in the database.
sp_grantdbaccess [@loginame =] 'login'
[,[@name_in_db =] 'name_in_db' [OUTPUT]]
0 (success) or 1 (failure)
SQL Server usernames can contain from 1 to 128 characters, including letters, symbols, and numbers. However, usernames cannot:
The security account must be granted access to the current database before it can use the database. Only accounts in the current database can be managed using sp_grantdbaccess. To remove an account from a database, use sp_revokedbaccess.
A security account for guest can be added if it does not already exist in the current database, and the login is also guest.
The sa login cannot be added to a database.
sp_grantdbaccess cannot be executed from within a user-defined transaction.
Only members of the db_accessadmin or db_owner fixed database roles can execute sp_grantdbaccess.
This example adds an account for the Windows NT user Corporate\GeorgeW to the current database and gives it the name Georgie.
EXEC sp_grantdbaccess 'Corporate\GeorgeW', 'Georgie'
sp_revokedbaccess | System Stored Procedures |