Display VIEW definition using T-SQL code (3 different ways)

Sometime back, I had posted about 3 Different Ways to display VIEW definition. Let us see some additional ways of displaying VIEW definition using T-SQL and some advantages/disadvantages of using these different methods. Thanks to Madhivanan on his inputs in the original posts.

I am using the AdventureWorks database:

Method 1: sp_helptext

sp_helptext 'Purchasing.vVendor'


Although this method is short, however there is no easy way (cursors required) to use the result of this query in another SQL query.


Method 2: sys.comments


SELECT sysco.text as [View Definition]
FROM sys.syscomments sysco
JOIN sys.objects sysob ON sysco.id = sysob.object_id
JOIN sys.schemas syssh ON sysob.schema_id = syssh.schema_id
WHERE sysob.name = 'vVendor'
AND syssh.name = 'Purchasing';

We get the same results as in sp_helptext, except here we can use the result of this query in another SQL query. The disadvantage here is that this method is lengthy and you have to specify the object name and schema name separately.


Method 3: OBJECT_DEFINITION


SELECT OBJECT_DEFINITION 
(OBJECT_ID(N'Purchasing.vVendor'))
as [View Definition]

This is one of my favorite methods as the result obtained from this method can be used in another query. Moreover the query is shorter.

OUTPUT (Same for all the three methods)

image

Note: It is also very easy to see the definitions of all VIEWS in your database using OBJECT_DEFINITION (as suggested by Madhivanan in the original post).


SELECT object_definition(object_id) as [View Definition]
FROM sys.objects
where type='V'

OUTPUT

image


About The Author

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigous Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook

3 comments:

Mr Kumar said...

Thanks for the informative article. however, I have a situation where I my view are fairly complex with multiple joins and "Union". How can i extract the column information? I need to know that a column "x" is coming from "tableA"."ColumnB"? Can we get this extracted from the metadata / view definition?

Delnar said...

I ran into a very similar situation. In my case the Columns that I was pivoting ended with two digits (IE: 01, 02, 03, etc). So I placed a where clause on my pivot table

where RIGHT(COL_NO, 2) = RIGHT(COL_AMT, 2)

So that I would only show the records that both came from the same column.

For completeness sake, here's my entire SQL statement.


SELECT rtrim(ltrim(hr_pe_id)) as Id,
PYH_NO as CdhNo,
cast(PYH_AMT as bigint) as Amt,
pyh_per_cc as PayPeriod,
py_per_ccyy as PeriodYYCC,
pyh_post_dt as PostDate,
pyh_num_cd as numcd,
pyh_rec_type as rectype,
pyh_tc_batch as batchId,
unique_key as unique_key,
pyh_ck_dt as CheckDate
from [finance].[dbo].pyh_hst_dtl
UNPIVOT (PYH_NO FOR COL_NO IN(
PYH_NO01,PYH_NO02,PYH_NO03,PYH_NO04,PYH_NO05,
PYH_NO06,PYH_NO07,PYH_NO08,PYH_NO09,PYH_NO10,
PYH_NO11,PYH_NO12,PYH_NO13,PYH_NO14,PYH_NO15)) AS CDH_NO
UNPIVOT (PYH_AMT FOR COL_AMT IN(
PYH_AMT01,PYH_AMT02,PYH_AMT03,PYH_AMT04,PYH_AMT05,
PYH_AMT06,PYH_AMT07,PYH_AMT08,PYH_AMT09,PYH_AMT10,
PYH_AMT11,PYH_AMT12,PYH_AMT13,PYH_AMT14,PYH_AMT15)) AS CDH_AMT
where RIGHT(COL_NO, 2) = RIGHT(COL_AMT, 2)
AND PYH_NO <> 0


I hope this helps.

Suprotim Agarwal said...

Thanks Joshua for sharing your solution!