Granting a user access to a database involves three steps. First, you create a login. The login lets the user connect to the SQL Server Database Engine. Then you configure the login as a user in the specified database. And finally, you grant that user permission to database objects. This article shows you these three steps, and shows you how to create a view and a stored procedure as the object.
To complete this tutorial, you need SQL Server Management Studio and access to a SQL Server instance.
- Install SQL Server Management Studio.
If you don’t have access to a SQL Server instance, select your platform from the following links. If you choose SQL Authentication, use your SQL Server login credentials.
- Windows: Download SQL Server 2017 Developer Edition.
- macOS: Download SQL Server 2017 on Docker.
Create a login
To access the Database Engine, users require a login. The login can represent the user’s identity as a Windows account or as a member of a Windows group, or the login can be a SQL Server login that exists only in SQL Server. Whenever possible you should use Windows Authentication.
By default, administrators on your computer have full access to SQL Server. For this lesson, we want to have a less privileged user; therefore, you will create a new local Windows Authentication account on your computer. To do this, you must be an administrator on your computer. Then you will grant that new user access to SQL Server.
Create a new Windows account
- Click Start, click Run, in the Open box, type %SystemRoot%\system32\compmgmt.msc /s, and then click OK to open the Computer Management program.
- Under System Tools, expand Local Users and Groups, right-click Users, and then click New User.
- In the User name box type Mary.
- In the Password and Confirm password box, type a strong password, and then click Create to create a new local Windows user.
Create a SQL login
In a Query Editor window of SQL Server Management Studio, type and execute the following code replacing
computer_name with the name of your computer.
FROM WINDOWS indicates that Windows will authenticate the user. The optional
DEFAULT_DATABASE argument connects
Mary to the
TestDatadatabase, unless her connection string indicates another database. This statement introduces the semicolon as an optional termination for a Transact-SQL statement.
<span class="hljs-keyword">CREATE</span> LOGIN [computer_name\Mary]
<span class="hljs-keyword">FROM</span> WINDOWS
<span class="hljs-keyword">WITH</span> DEFAULT_DATABASE = [TestData];
This authorizes a user name
Mary, authenticated by your computer, to access this instance of SQL Server. If there is more than one instance of SQL Server on the computer, you must create the login on each instance that
Mary must access.
Mary is not a domain account, this user name can only be authenticated on this computer.
Grant access to a database
Mary now has access to this instance of SQL Server, but does not have permission to access the databases. She does not even have access to her default database TestData until you authorize her as a database user.
To grant Mary access, switch to the TestData database, and then use the CREATE USER statement to map her login to a user named Mary.
To create a user in a database
Type and execute the following statements (replacing
computer_name with the name of your computer) to grant
Mary access to the
<span class="hljs-keyword">USE</span> [TestData];
<span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">USER</span> [Mary] <span class="hljs-keyword">FOR</span> LOGIN [computer_name\Mary];
Now, Mary has access to both SQL Server and the
Create views and stored procedures
As an administrator, you can execute the SELECT from the Products table and the vw_Names view, and execute the pr_Names procedure; however, Mary cannot. To grant Mary the necessary permissions, use the GRANT statement.
Grant permission to stored procedures
Execute the following statement to give
EXECUTE permission for the
pr_Names stored procedure.
<span class="hljs-keyword">GRANT</span> <span class="hljs-keyword">EXECUTE</span> <span class="hljs-keyword">ON</span> pr_Names <span class="hljs-keyword">TO</span> Mary;
In this scenario, Mary can only access the Products table by using the stored procedure. If you want Mary to be able to execute a SELECT statement against the view, then you must also execute
GRANT SELECT ON vw_Names TO Mary. To remove access to database objects, use the REVOKE statement.
If the table, the view, and the stored procedure are not owned by the same schema, granting permissions becomes more complex.
You must have EXECUTE permission to execute a stored procedure. You must have SELECT, INSERT, UPDATE, and DELETE permissions to access and change data. The GRANT statement is also used for other permissions, such as permission to create tables.