SeanKilleen/seankilleen.github.io

How to Create a SQL Server user with access to certain tables

Closed this issue · 0 comments

-- Replace THE_USERNAME and password
CREATE LOGIN THE_USERNAME WITH password='A_SECURE_PASSWORD';

-- Make the user the same as the login
CREATE USER THE_USERNAME FROM LOGIN THE_USERNAME;

-- Create a role and give the user access
CREATE ROLE THE_ROLE AUTHORIZATION THE_USER;

-- Just to make sure the user has access
EXEC sp_addrolemember 'THE_ROLE', 'THE_USER';

-- The last part -- granting permissions to the role
GRANT SELECT,UPDATE,INSERT,DELETE,EXECUTE ON dbo.TABLE_1 TO THE_USER;
GRANT SELECT,UPDATE,INSERT,DELETE,EXECUTE ON dbo.TABLE_2 TO THE_USER;