Monday, September 9, 2024

How do I grant a user access to a specific database in SQL Server?

 To grant access to a user for a specific database, use the following commands:

Create a login (if it doesn't already exist):

CREATE LOGIN YourLoginName WITH PASSWORD = 'YourPassword';

Grant access to the database:

USE YourDatabaseName; CREATE USER YourLoginName FOR LOGIN YourLoginName;

Grant a role or specific permissions:

EXEC sp_addrolemember 'db_datareader', 'YourLoginName';  -- Grants read access