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
51 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:
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DTS Connection Failed 7 70
Querying data from 3 SQL tables 2 32
sql select record as one long string 21 24
SQL Server: Unable to remove duplicate sets in Header/Detail 6 24
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

810 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