How to Create a View in SQL Server

To create a view in SQL Server:

  1. Open a new query by clicking the New Query button in the SSMS toolbar
  2. Type or paste a CREATE VIEW statement (example below)
  3. Run the script

The view will now be created in the database. You will be able to see it under the Views node in the Object Explorer.

You can now use SELECT statements against the view in future queries.

Below are screenshots of the above steps.

Open a New Query

Open a new query by clicking the New Query button in the SSMS toolbar:

Screenshot of the SSMS with the New Query button highlighted (SQL Server 2014)
The SSMS with the New Query button highlighted.

 

Type or paste a CREATE VIEW statement

This will start with CREATE VIEW (view_name) AS followed by the contents of the view.

Here’s an example:

CREATE VIEW ToDoList AS
SELECT	Tasks.TaskName, Tasks.Description
FROM	Status INNER JOIN
			Tasks ON Status.StatusId = Tasks.StatusId
WHERE	(Status.StatusId = 1)

Run the script

Screenshot of a SQL view script in SQL Server 2014
A SQL view called “ToDoList” has just been created, and now appears in the Object Explorer.

Run the View

You can run a view by selecting from it. Use the SELECT statement in the same way that you’d use it when selecting the contents of a table.

Screenshot of running a SQL view in SQL Server 2014
Run a view by using a SELECT statement.

So a view is basically a pre-written query that is stored on the database. Instead of typing the whole query out every time you want to run it, you can save it in a view. Then all you need to do is run a simple SELECT against that view.