To create a view in SQL Server:
- Open a new query by clicking the New Query button in the SSMS toolbar
- Type or paste a CREATE VIEW statement (example below)
- 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:
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
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.
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.