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
46 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
ID: 41802329
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
ID: 41803922
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
ID: 41803948
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
ID: 41805066
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
ID: 41805086
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

867 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