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
66 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher 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:Scott Pletcher
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

626 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