Published on

September 30, 2007

Exploring the Power of SQL Server Views

Views in SQL Server are often seen as a way to abstract complicated joins, but their usefulness goes beyond that. In this article, we will explore some scenarios where views prove to be incredibly useful and provide a deeper understanding of their capabilities.

Basic Syntax and Joining Tables

Let’s start with the basic syntax of a view and how it can simplify working with multiple tables. Consider the following example from the AdventureWorks database:

CREATE VIEW [Sales].[vIndividualCustomer] 
AS 
SELECT 
    i.[CustomerID],
    c.[Title],
    c.[FirstName],
    c.[MiddleName],
    c.[LastName],
    c.[Suffix],
    c.[Phone],
    c.[EmailAddress],
    c.[EmailPromotion],
    at.[Name] AS [AddressType],
    a.[AddressLine1],
    a.[AddressLine2],
    a.[City],
    [StateProvinceName] = sp.[Name],
    a.[PostalCode],
    [CountryRegionName] = cr.[Name],
    i.[Demographics]
FROM [Sales].[Individual] i
    INNER JOIN [Person].[Contact] c 
    ON c.[ContactID] = i.[ContactID]
    INNER JOIN [Sales].[CustomerAddress] ca 
    ON ca.[CustomerID] = i.[CustomerID]
    INNER JOIN [Person].[Address] a 
    ON a.[AddressID] = ca.[AddressID]
    INNER JOIN [Person].[StateProvince] sp 
    ON sp.[StateProvinceID] = a.[StateProvinceID]
    INNER JOIN [Person].[CountryRegion] cr 
    ON cr.[CountryRegionCode] = sp.[CountryRegionCode]
    INNER JOIN [Person].[AddressType] at 
    ON ca.[AddressTypeID] = at.[AddressTypeID]
WHERE i.[CustomerID] IN (SELECT [Sales].[Customer].[CustomerID] 
    FROM [Sales].[Customer] WHERE UPPER([Sales].[Customer].[CustomerType]) = 'I');

This view combines data from multiple tables to provide a simplified representation of individual customers. It demonstrates how views can make complex queries more manageable and easier to work with.

Operations on Views

One common question about views is what operations can be performed on them. The answer is that it depends on the view definition. For example, deleting rows from a view may or may not be allowed, depending on the view’s underlying query. Updating a view, on the other hand, is generally possible. Inserts, however, are usually not allowed on views.

It’s important to note that views can sometimes be modified using instead of triggers, although this is not a common practice. Adding an instead of trigger to a view can provide more flexibility in terms of data manipulation.

Referencing Other Databases or Servers

Views can be a powerful tool for referencing data from other databases or servers. Let’s say we have an Orders database and a Corporate database, and we need to pull information from the Employees table in the Corporate database. Instead of directly referencing the table, we can create a view that encapsulates the dependency:

CREATE VIEW Employees AS
SELECT * FROM Corporate.dbo.Employees;

CREATE VIEW vOrders AS
SELECT * FROM dbo.OrderDetails OD
INNER JOIN Employees E ON OD.EmployeeID = E.EmployeeID;

By using views, we isolate the dependency on another database or server in one place. This allows for easier maintenance and flexibility in case of server or database migrations.

Hiding Changes and Improving Performance

Another scenario where views can be useful is when we want to hide changes to a table from users. For example, if adding a column to a table would break an off-the-shelf application, we can create a view that only returns the original set of columns from a renamed table. This way, the application can continue to function without being aware of the underlying changes.

Views can also be used to improve performance when loading large amounts of data. By creating multiple tables and switching between them using a view, we can minimize downtime for users. For example, if we have a table called OrderHistory that takes a long time to truncate and reload, we can create two tables (OrderHistoryA and OrderHistoryB) and a view (OrderHistory) that initially points to OrderHistoryA. We can then perform the data loading on OrderHistoryB and switch the view to point to it, minimizing the downtime for users.

Conclusion

Views in SQL Server offer more than just a way to abstract complicated joins. They provide a powerful tool for referencing data from other databases or servers, hiding changes from users, and improving performance. By understanding the possibilities and utilizing views effectively, you can simplify your database design and enhance the overall functionality of your SQL Server environment.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.