Solved

List of SQL VIEWS, with their Columns values plus the Constraints and Extended Properties  - not from sys.views, but from  sys.tables.

Posted on 2016-09-16
5
42 Views
Last Modified: 2016-09-19
My present query list the VIEWS by Schema, View, Column Name, etc.  I have tried, but it must be Friday afternoon kicking in and drawing a blank on how to display the columns' Constraints and Extended Properties.  Not the Constraints and Extended Properties from the sys.views, but the values that are present in the table, sys.table.  

If you look at my attached query and replace sys.views with sys.tables on Line 38 and change 'U' to a 'V' on Line 45 that is what I want to see, except it's listing the views and not tables.

I have attached a file to create the Test Table with Extended Properties and a file to create the View along with my query to list all views.
List-of-views_columns_extended.sql
Create-Table.sql
Create-View.sql
0
Comment
Question by:wdbates
  • 3
  • 2
5 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
Comment Utility
I've modified it to use "sys.objects" rather than "sys.sysobjects", and I changed the column "ViewName" to "TableName":


SELECT      
        S.name       AS SchemaName
      , obj.name     AS TableName
      , col.name     AS ColumnName
      , col.colorder AS Col#
      , sys.systypes.name +
      CASE
             WHEN systypes.name LIKE 'n%char' THEN ' (' + CONVERT(nvarchar, col.length / 2) + ')'
         WHEN systypes.name LIKE '%char%' THEN ' (' + CONVERT(nvarchar, col.length) + ')'
         ELSE ''
      END AS DataType,
      CASE col.isnullable
           WHEN 0 THEN 'NOT NULL'
           ELSE ''
      END AS Nullable,
      CASE
         WHEN COLUMNPROPERTY(obj.object_id, col.name, 'IsIdentity') = 1 THEN 'IDENTITY '
         WHEN conobj.type = 'D' THEN 'DEFAULT ' + syscomments.text
         WHEN conobj.type = 'F' THEN 'REFERENCES ' + fkobj.name + '.' + fkcol.name
         ELSE ''
      END AS Constraints,
      CASE
             WHEN ISNULL(ep.value, 0) = 0 THEN ''
             ELSE ep.value
        END AS 'Extended Properties'
        --select * from sys.sysobjects
FROM sys.objects AS obj
INNER JOIN sys.syscolumns AS col ON obj.object_id = col.id
INNER JOIN sys.systypes          ON sys.systypes.xtype = col.xtype
LEFT OUTER JOIN sys.sysconstraints AS con
      INNER JOIN sys.objects    AS conobj ON con.constid = conobj.object_id
      ON con.id = obj.object_id AND con.colid = col.colid
LEFT OUTER JOIN sys.syscomments ON conobj.object_id = sys.syscomments.id
LEFT OUTER JOIN sys.sysforeignkeys AS fk
      INNER JOIN sys.syscolumns AS fkcol
            ON fk.rkeyid = fkcol.id AND fk.rkey = fkcol.colid
      INNER JOIN sys.objects AS fkobj ON fkcol.id = fkobj.object_id
      ON conobj.object_id = fk.constid AND obj.object_id = fk.fkeyid AND col.colid = fk.fkey
INNER JOIN sys.schemas AS S
      ON S.schema_id = obj.schema_id
LEFT OUTER JOIN sys.extended_properties AS ep
    ON obj.object_id = ep.major_id
     AND col.colorder = ep.minor_id
WHERE (obj.type = 'U')
AND   (sys.systypes.name <> 'sysname')
ORDER BY S.name, obj.name, col.colid
0
 

Author Closing Comment

by:wdbates
Comment Utility
Dear Scott;

Sorry for the delay as this was one of the weekends that I and about 10 other guys go out and build wheelchair ramps and do home repairs for the elderly and handicap.  But,  as always you have the very best solutions and this one is perfect.

Thank you again.

William
0
 

Author Comment

by:wdbates
Comment Utility
Hey Scott;

I jumped the gun on closing this case.  I'm looking for the same results as this query, listing the columns and their attributes of tables, but listing the columns and their attributes of Views.  It sounds simple, by just changing the value WHERE (obj.type = 'U') to WHERE (obj.type = 'V') and changing sys.tables to sys.views.  Yes that works, but I'm not getting the Constraints and Extended Properties.  I need the Constraints and Extended Properties that are in sys.tables that matches to the same columns in the Views.

Thanks
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Let me look at this.

Constraints not showing up makes sense, since constraints don't exist on view columns, they exist on the underlying table column.

But extended properties should definitely exist for views.  The join to sys.extended_properties should be on col.id and not col.colorder: there's not even any clear documentation on what colorder is.

In fact, I'd strongly suggest getting rid of all the ancient "sys" views used in the query:
replacing sys.syscolumns with sys.columns;
replacing sys.systypes with sys.types;
etc.
I haven't used those views since SQL 2000, so I don't really remember them at all.
0
 

Author Comment

by:wdbates
Comment Utility
Thank you Scott.  Old habits are hard to break using sys...and I go all the way back prior to DBase III.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now