Using Catalog Views – sys.views


sys.views returns a row for each view in the database, sys.views only returns rows for which the user owns or has been granted permission for. Below are some of the columns returned by sys.views.

1. Name – Give name of the view,

2. Schema_id – ID of schema to which the view belongs,

3. Type – 'V' for View,

4. Type_Desc – Textual description of the type,

5. Create_Date – Creation date/time of the view,

6. Modify_Date – Last modification date/time for the view,

7. With_Check_Option – Specifies whether the VIEW is created by specifying WITH CHECK OPTION.

For example,

— © 2011 – Vishal (http://SqlAndMe.com)

 

USE AdventureWorks2008R2

 

SELECT      Name, Schema_ID, Type_Desc,

            Create_Date, Modify_Date, With_Check_Option

FROM        sys.views

Partial Result Set:

Name                       Schema_ID     Type_Desc     Create_Date                 Modify_Date                 With_Check_Option

————————-  ———–   ———–   ————————-  ————————-      ————————

vAdditionalContactInfo     6             VIEW          2010-12-04 12:29:05.110    2010-12-04 12:29:05.110       0

vEmployee                  5             VIEW          2010-12-04 12:29:05.127    2010-12-04 12:29:05.127       0

vEmployeeDepartment        5             VIEW          2010-12-04 12:29:05.130    2010-12-04 12:29:05.130       0

vIndividualCustomer        9             VIEW          2010-12-04 12:29:05.133    2010-12-04 12:29:05.133       0

vPersonDemographics        9             VIEW          2010-12-04 12:29:05.150    2010-12-04 12:29:05.150       0

vJobCandidate              5             VIEW          2010-12-04 12:29:05.170    2010-12-04 12:29:05.170       0

This view does not return the VIEW definition. To retrieve the VIEW definition, you can user OBJECT_DEFINITION function. This function takes one argument – Object ID, which can be retrieved from sys.views or by using OBJECT_ID function:

SELECT      OBJECT_DEFINITION(OBJECT_ID(N'HumanResources.vEmployee'))

            AS 'Object Definition'

Result Set:

Object Definition

—————————————————-

CREATE VIEW [HumanResources].[vEmployee]

AS

SELECT

e.[BusinessEntityID]

,p.[Title]

,p.[FirstName]

,p.[MiddleName]

,p.[LastName]

,p.[Suffix]

,e.[JobTitle] 

,pp.[PhoneNumber]

,pnt.[Name] AS [PhoneNumberType]

,ea.[EmailAddress]

,p.[EmailPromo

 

(1 row(s) affected)

Hope This Helps!

Vishal

  1. No comments yet.
  1. No trackbacks yet.