SQL Server Views Tutorial
SQL Server views provide database developers with a pre-compiled query that returns several rows. Views are created when you have the same queries applied in the database application. Because views are pre-compiled, they run faster and improve performance on the database server. The view returns several rows, and you use your database queries to filter the results to meet your requirements. Views are created using the "create view" syntax.
Instructions
-
-
1
Create your new view name. Each view is given a name which is used to query the database from your applications. The code starts the "create view" code:
create view view_Customer
-
2
Add your view's SQL code. In this example, the view retrieves all customers and their corresponding orders. The code added to a view is similar to the syntax used to create stored procedures. The following SQL code creates a view that retrieves all customer information and orders:
create view view_Customer
as
select * from customer join order on customer.customerId=order.customerId -
-
3
Press the "F5" key to execute the statement. This creates the view on your SQL Server.
-
4
Call the view from your SQL code. Now that the view is created, you can use it in your stored procedures. You can retrieve all the records from the view, or you can filter results similar to retrieving only some rows from a table. The code below retrieves customer information whose ID is 2:
select * from view_Customer where customerId=2
-
1
References
- Photo Credit rackmount server farm datacenter image by Adryn from Fotolia.com