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.

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 />

WHERE CAST(soh.OrderDate AS Date) =<br />

CAST(DATEADD(day,-1,GETDATE()) AS Date)<br />

Now let’s try it the other way — using the sys.sql_modules metadata function. The only major hassle<br />

with using this function is that all the objects are coded in object IDs.<br />

Object IDs are <strong>SQL</strong> <strong>Server</strong>’s internal way of keeping track of things. They are integer<br />

values rather than the names that you’re used to for your objects. In general, they<br />

are outside the scope of this book, but it is good to realize they are there, as you will<br />

find them used by scripts you may copy from other people or just bump into them<br />

later in your <strong>SQL</strong> endeavors.<br />

Fortunately, you can get around this by using the OBJECT_ID() function:<br />

SELECT *<br />

FROM sys.sql_modules<br />

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

Again, you get the same block of code (indeed, all sp_helptext does is run what amounts to this same<br />

query).<br />

Note that, by default, the Results in Text option in the Query window limits the<br />

results from any individual column to just 256 characters, so running the previous<br />

sys.sql_modules query may get you a truncated version of the text for the view. If<br />

you look at it in grid mode, the limit is 64k, so everything comes through. You can<br />

change the maximum number of characters per column by going to Tools ➪<br />

Options ➪ Query Results ➪ <strong>SQL</strong> <strong>Server</strong> ➪ Results to Text and changing the appropriate<br />

setting.<br />

Protecting Code: Encr ypting V iews<br />

If you’re building any kind of commercial software product, odds are that you’re interested in protecting<br />

your source code. Views are the first place we see the opportunity to do just that.<br />

All you have to do to encrypt your view is use the WITH ENCRYPTION option. This one has a couple of<br />

tricks to it if you’re used to the WITH CHECK OPTION clause:<br />

❑ WITH ENCRYPTION goes after the name of the view, but before the AS keyword<br />

❑ WITH ENCRYPTION does not use the OPTION keyword<br />

Chapter 10: Views<br />

317

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

Saved successfully!

Ooh no, something went wrong!