How to create user-defined role in SQL Server Database?
How to create user-defined role in SQL Server Database?
15916-Jul-2024
Updated on 16-Jul-2024
Home / DeveloperSection / Forums / How to create user-defined role in SQL Server Database?
How to create user-defined role in SQL Server Database?
Ashutosh Kumar Verma
16-Jul-2024SQL Server User-Defined Role
The following example creates a new user and role, grants permissions to the role, and adds a user to the role.
First, set the current database to
master
and create a new login calledkrishna
Second, switch the current database to
MyCollegeDb
and create a new user calledkrishna
for login,Create Role
Create a new role called
emp_report
in theMyCollegeDb
databaseIn this example, we use the
CREATE ROLE
statement to create a new role in theMyCollegeDb
database. Theemp_report
is the role name.Grant Permission to the Role
Grant the
SELECT
privilege on thedbo
schema to theemp_report
Add User to the Role
Add the
krishna
user to theemp_report
roleFinally, connect to the
MyCollegeDb
database using userkrishna
. In this case, userkrishna
can only view tables in thedbo
schema. Also, userkrishna
can only select data from tables in thisdbo
schema because the user is a member ofemp_report
which has theSELECT
privilegeLet's try to update the records in the above table using the SQL query to verify the granted permission,
In the above case the SQL Server denied permission to update the data.
Also, Read: Describe the different types roles in SQL Server.