Published on

August 6, 2011

Understanding Views and Encrypting Views in SQL Server

Views are a powerful feature in SQL Server that allow you to create virtual tables based on the result of a query. They provide a way to simplify complex queries and make it easier to retrieve and manipulate data. However, there are certain rules and considerations that need to be followed when working with views.

View Rules

Not every query can be turned into a view. There are specific rules that must be followed before a query can be converted into a view. One common rule is that every column in the query must have a name. For example, if you have a query that includes an aggregation function like SUM, you need to provide an alias for the aggregated column. Otherwise, you will encounter an error when trying to create the view.

Here’s an example of a query that cannot be turned into a view:

CREATE VIEW dbo.vEmpGrantTotals
AS
SELECT EmpID, SUM(Amount)
FROM [Grant]
GROUP BY EmpID

In this case, the error message will indicate that no column name was specified for column 2. To resolve this, you need to alias the expression field as “TotalAmount” and then run the CREATE VIEW statement again.

CREATE VIEW dbo.vEmpGrantTotals
AS
SELECT EmpID, SUM(Amount) AS TotalAmount
FROM [Grant]
GROUP BY EmpID

Encrypting Views

Sometimes, you may want to ensure that people can use a view to run reports, but you don’t want them to be able to see or recreate the underlying code. In such cases, you can encrypt the view’s source code. This can be done by making two modifications to the code for the view:

  1. Change CREATE VIEW to ALTER VIEW.
  2. Add WITH ENCRYPTION before the AS keyword.

Here’s an example of how to encrypt a view:

ALTER VIEW dbo.vEmpGrantTotals
WITH ENCRYPTION
AS
SELECT EmpID, SUM(Amount) AS TotalAmount
FROM [Grant]
GROUP BY EmpID

After encrypting the view, the source code will no longer be visible. You will see a small padlock icon on the view in Object Explorer, indicating that it is encrypted. Additionally, if you try to script the view using Management Studio (SSMS), you will receive a message stating that the text is encrypted and cannot be scripted.

It’s important to note that encrypting a view does not affect its functionality. You can still use the view to retrieve data and perform operations on it.

Conclusion

Views are a valuable tool in SQL Server that allow you to simplify complex queries and make it easier to work with data. By following the rules for creating views and understanding how to encrypt them, you can enhance the security and usability of your database.

Do you have any questions or comments about views and encrypting views in SQL Server? Leave your answer in the comment section below along with your country of residence for a chance to win a copy of Joes 2 Pros Volume 4. One winner will be announced each day from the United States and India.

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.