Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
71 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 70

Accepted Solution

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

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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

926 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