Sie sind auf Seite 1von 6

7-22

Module 7: Implementing Views

Practice: Creating a View

The purpose of this practice is to enable you to create a view by using Transact-SQL.

Objectives
In this practice, you will create a view by using Transact-SQL.

Instructions

Start the 2779B-MIA-SQL-07 virtual machine.

Log on to the virtual machine with the user name Student and the password
Pa$$w0rd.

Create a view
1. Click Start, point to All Programs, point to Microsoft SQL Server 2005, and then
click SQL Server Management Studio.
2. In the Connect to Server dialog box, specify the values in the following table, and
then click Connect.
Property

Value

Server type

Database Engine

Server name

MIAMI

Authentication

Windows Authentication

3. If Object Explorer is not visible, on the View menu, click Object Explorer.
4. In Object Explorer, expand Databases, expand AdventureWorks, and then expand
Views.
5. On the File menu, point to Open, and then click File.

Module 7: Implementing Views

7-23

6. In the Open File dialog box, browse to the D:\Practices folder, click the
CreateView.sql query file, and then click Open.
7. In the Connect to Database Engine dialog box, specify the values in the following
table, and then click Connect.
Property

Value

Server name

MIAMI

Authentication

Windows Authentication

8. Review the following code in the query window.


USE AdventureWorks
GO
CREATE VIEW [HumanResources].[vEmployeeContact]
WITH ENCRYPTION AS SELECT
e.EmployeeID,
e.Title,
c.Title AS Salutation,
c.FirstName,
c.MiddleName,
c.LastName,
c.EmailAddress,
c.Phone
FROM Person.Contact c INNER JOIN HumanResources.Employee e
ON c.ContactID = e.ContactID

9. On the toolbar, click Execute.


10. When the command has completed successfully, in Object Explorer, right-click the
Views folder, and then click Refresh to verify that the
HumanResources.vEmployeeContact view has been created.
11. On the toolbar, click New Query.
12. In the new, blank query window, type the following Transact-SQL code.
USE [AdventureWorks]
GO
SELECT * FROM
[HumanResources].[vEmployeeContact]

13. On the toolbar, click the Execute.


14. Verify that data is returned from the view.
15. Close SQL Server Management Studio. Click No if prompted to save any files.
After you complete the practice, you must shut down the 2779B-MIA-SQL-07 virtual
machine and discard any changes.
Important: If the Close dialog box appears, ensure that Turn off and delete
changes is selected, and then click OK.

7-30

Module 7: Implementing Views

Lab: Implementing Views

After completing this lab, you will be able to:

Create a view.

Create an indexed view.

Create a partitioned view.

Estimated time to complete this lab: 45 minutes

Lab Setup
For this lab, you will use the available virtual machine environment. Before you begin the
lab, you must:

Start the 2779B-MIA-SQL-07 virtual machine.

Log on to the virtual machine with the user name Student and the password
Pa$$w0rd.

Lab Scenario
After earlier consultation with the Human Resources department, the senior database
developer has identified a number of views that will improve database use and simplify
future development. The senior database developer has asked you to perform the
following tasks:

Create a new view named HumanResources.vEmployeeDetails that uses the


following SELECT logic. Remember to use the SCHEMABINDING option when
you create the view, because you will need to create an index on the view later.

SELECT

Module 7: Implementing Views

7-31

e.[EmployeeID]
,c.[Title]
,c.[FirstName]
,c.[MiddleName]
,c.[LastName]
,c.[Suffix]
,e.[Title] AS [JobTitle]
,c.[Phone]
,c.[EmailAddress]
,c.[EmailPromotion]
,a.[AddressLine1]
,a.[AddressLine2]
,a.[City]
,sp.[Name] AS [StateProvinceName]
,a.[PostalCode]
,cr.[Name] AS [CountryRegionName]
,c.[AdditionalContactInfo]
FROM [HumanResources].[Employee] e
INNER JOIN [Person].[Contact] c
ON c.[ContactID] = e.[ContactID]
INNER JOIN [HumanResources].[EmployeeAddress] ea
ON e.[EmployeeID] = ea.[EmployeeID]
INNER JOIN [Person].[Address] a
ON ea.[AddressID] = a.[AddressID]
INNER JOIN [Person].[StateProvince] sp
ON sp.[StateProvinceID] = a.[StateProvinceID]
INNER JOIN [Person].[CountryRegion] cr
ON cr.[CountryRegionCode] = sp.[CountryRegionCode]

After testing the HumanResources.vEmployeeDetails view, turn it into an indexed


view by creating a unique clustered index named IX_vEmployeeDetails on the
EmployeeID column.

Create a distributed partitioned view named Person.vContact on the AW_Contacts


database. The view should union the sets of data contained in the following source
servers and tables:

[MIAMI].AW_Contacts.Person.Contact

[MIAMI\SQLINSTANCE2].AW_Contacts.Person.Contact

[MIAMI\SQLINSTANCE3].AW_Contacts.Person.Contact

Additional Information
When performing database development tasks, it can be helpful to use SQL Server
Management Studio to create a SQL Server Scripts project, and use it to document the
Transact-SQL code necessary to re-create the solution if necessary.
Use the following procedure to create a SQL Server Scripts project:
1. Open SQL Server Management Studio, connecting to the server you want to manage.
2. On the File menu, point to New, and then click Project.

7-32

Module 7: Implementing Views

3. Select the SQL Server Scripts template and enter a suitable name and location for
the project. Note that you can create a solution that contains multiple projects, but in
many cases a single project per solution is appropriate.
Use the following procedure to add a query file to a project:
1. On the Project menu, click New Query, or in Solution Explorer, right-click the
Queries folder, and then click New Query. If Solution Explorer is not visible, on the
View menu, click Solution Explorer.
2. When prompted, connect to the server on which you want to execute the query. This
will add a connection object to the project.
3. To change the name of the query file from the default name (SQLQuery1.sql), rightclick it in Solution Explorer and click Rename.
Although you can perform all database development tasks by executing Transact-SQL
statements, it is often easier to use the graphical user interface in SQL Server
Management Studio. However, you should generate the corresponding Transact-SQL
scripts and save them in the project for future reference.
Often, you can generate the Transact-SQL script for an action before clicking OK in the
Properties dialog box used to perform the action. Many Properties dialog boxes include a
Script list with which you can script the action to a new query window, a file, the
Clipboard, or a SQL Server Agent job. A common technique is to add a blank query file
to a project, script each action to the Clipboard as it is performed, and then paste the
generated script into the query file.
You can also generate scripts for many existing objects, such as databases and tables. To
generate a script, right-click the object in Object Explorer and script the CREATE action.
If Object Explorer is not visible, on the View menu, click Object Explorer.

Module 7: Implementing Views

7-33

Exercise 1: Creating Views


In this exercise, you will create a view named HumanResources.vEmployeeDetails that
displays employee name and address information.
The principal tasks for this exercise are:

Create a SQL Server Scripts project.

Create the HumanResources.vEmployeeDetails view.

Test the HumanResources.vEmployeeDetails view.

Tasks

Supporting information

1.

Create a new SQL Server Scripts project named AW_Views in the


D:\Labfiles\Starter folder.

Add a new query to the project, connecting to MIAMI by using


Microsoft Windows authentication when prompted.

Rename the new query file CreateEmployeeView.sql.

In the query window, type the appropriate Transact-SQL statement to


create the HumanResources.vEmployeeDetails view in the
AdventureWorks database.

Execute the query, and then save the query file.

Create a new query that displays all the results from the
HumanResources.vEmployeeDetails view.

Execute the query and verify that data is returned from the view.

2.

3.

Create a SQL Server


Scripts project.

Create the
HumanResources.vEm
ployeeDetails view.

Test the
HumanResources.vEm
ployeeDetails view.

Das könnte Ihnen auch gefallen