17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10: Views<br />

318<br />

In addition, remember that if you use an ALTER VIEW statement, you are entirely replacing the existing<br />

view except for access rights. This means that the encryption is also replaced. If you want the altered<br />

view to be encrypted, then you must use the WITH ENCRYPTION clause in the ALTER VIEW statement.<br />

Let’s do an ALTER VIEW on our CustomerOrders_vw view that we created in AdventureWorks<strong>2008</strong>. If<br />

you haven’t yet created the CustomerOrders_vw view, then just change the ALTER to CREATE:<br />

ALTER VIEW CustomerOrders_vw<br />

WITH ENCRYPTION<br />

AS<br />

SELECT sc.AccountNumber,<br />

soh.SalesOrderID,<br />

soh.OrderDate,<br />

sod.ProductID,<br />

pp.Name,<br />

sod.OrderQty,<br />

sod.UnitPrice,<br />

sod.UnitPriceDiscount * sod.UnitPrice * sod.OrderQty AS TotalDiscount,<br />

sod.LineTotal<br />

FROM Sales.Customer AS sc<br />

INNER JOIN Sales.SalesOrderHeader AS soh<br />

ON sc.CustomerID = soh.CustomerID<br />

INNER JOIN Sales.SalesOrderDetail AS sod<br />

ON soh.SalesOrderID = sod.SalesOrderID<br />

INNER JOIN Production.Product AS pp<br />

ON sod.ProductID = pp.ProductID;<br />

Now do an sp_helptext on your CustomerOrders_vw:<br />

EXEC sp_helptext CustomerOrders_vw<br />

<strong>SQL</strong> <strong>Server</strong> promptly tells us that it can’t do what we’re asking:<br />

The text for object ‘CustomerOrders_vw’ is encrypted.<br />

The heck you say, and promptly go to the sys.sql_modules metadata function:<br />

SELECT *<br />

FROM sys.sql_modules<br />

WHERE object_id = OBJECT_ID(‘dbo.CustomerOrders_vw’);<br />

But that doesn’t get you very far either — <strong>SQL</strong> <strong>Server</strong> recognizes that the table was encrypted and will<br />

give you a NULL result.<br />

In short — your code is safe and sound. Even if you pull it up in other viewers (such as the Management<br />

Studio, which actually won’t even give you the Modify option on an encrypted table), you’ll find it useless.<br />

Make sure you store your source code somewhere before using the WITH ENCRYPTION<br />

option. Once it’s been encrypted, there is no way to get it back. If you haven’t stored<br />

your code away somewhere and you need to change it, then you may find yourself<br />

re-writing it from scratch.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!