Solved

sql syntax to list column names and the description

Posted on 2004-08-27
7
736 Views
Last Modified: 2009-12-16
MS SQL Enterprise Manager allows descriptions to be stored with each coumn in table.  What is the SQL syntax to produce the same output as sp_columns but with the description included.
0
Comment
Question by:Kalvyn
[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
7 Comments
 
LVL 34

Expert Comment

by:arbert
ID: 11915177
YOu have to use the function fn_listextendedproperty to get the properties.
0
 
LVL 34

Expert Comment

by:arbert
ID: 11915184
There is an example in booksonline:

SELECT   *
FROM   ::fn_listextendedproperty (NULL, 'user', 'dbo', 'table', 'T1', 'column', default)


These are the parms:

fn_listextendedproperty (
    { default | [ @name = ] 'property_name' | NULL }
    , { default | [ @level0type = ] 'level0_object_type' | NULL }
    , { default | [ @level0name = ] 'level0_object_name' | NULL }
    , { default | [ @level1type = ] 'level1_object_type' | NULL }
    , { default | [ @level1name = ] 'level1_object_name' | NULL }
    , { default | [ @level2type = ] 'level2_object_type' | NULL }
    , { default | [ @level2name = ] 'level2_object_name' | NULL }
    )
0
 
LVL 43

Expert Comment

by:Eugene Z
ID: 11916350
something like this:

select c.COLUMN_NAME,
a.value ColumnDescription,
c.TABLE_CATALOG DatabaseName,
[TABLE_NAME], [COLUMN_DEFAULT], [IS_NULLABLE], [DATA_TYPE], [CHARACTER_MAXIMUM_LENGTH], [CHARACTER_OCTET_LENGTH], [NUMERIC_PRECISION], [NUMERIC_PRECISION_RADIX], [NUMERIC_SCALE], [DATETIME_PRECISION], [CHARACTER_SET_CATALOG], [CHARACTER_SET_SCHEMA], [CHARACTER_SET_NAME], [COLLATION_CATALOG], [COLLATION_SCHEMA], [COLLATION_NAME]

from sysproperties a
inner join
sysobjects b
on
a.id=b.id
inner join
syscolumns d
on b.id=d.id
inner join
INFORMATION_Schema.columns c

on d.name=c.COLUMN_NAME
where a.name='MS_Description' and b.name='YourTableName'
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 11916382
If you want to join it with all the stuff from sp_columns, I think you'll need to use a temp table to hold that info and then join to it, for example:


DECLARE @table SYSNAME
SET @table = N'sections'


IF OBJECT_ID('tempdb.dbo.#sp_columns') IS NOT NULL
      DROP TABLE #sp_columns
CREATE TABLE #sp_columns (
      TABLE_QUALIFIER sysname,
      TABLE_OWNER sysname,
      TABLE_NAME sysname,
      COLUMN_NAME sysname,
      DATA_TYPE smallint,
      TYPE_NAME varchar(13),
      [PRECISION] int,
      LENGTH int,
      SCALE smallint,
      RADIX smallint,
      NULLABLE smallint,
      REMARKS varchar(254),
      COLUMN_DEF nvarchar(4000),
      SQL_DATA_TYPE smallint,
      SQL_DATETIME_SUB smallint,
      CHAR_OCTET_LENGTH int,
      ORDINAL_POSITION int,
      IS_NULLABLE varchar(254),
      SS_DATA_TYPE tinyint
)

SET NOCOUNT ON
INSERT INTO #sp_columns
EXEC sp_columns @table
SET NOCOUNT OFF

SELECT spc.*, ext.value
FROM ::fn_listextendedproperty (NULL, 'user', 'dbo', 'table', 'T1', 'column', 'description') ext
RIGHT OUTER JOIN #sp_columns spc ON spc.column_name = ext.objname
0
 
LVL 43

Accepted Solution

by:
Eugene Z earned 250 total points
ID: 11916554
try this one - just remove what you do not need in select:

select *
FROM (sysobjects INNER JOIN syscolumns
ON syscolumns.id = sysobjects.id) LEFT JOIN sysproperties
ON sysproperties.smallid = syscolumns.colid AND
sysproperties.id = syscolumns.id
left join
INFORMATION_Schema.columns c

on sysproperties.name=c.COLUMN_NAME
where sysproperties.name='MS_Description' --and b.name='table1'
and sysobjects.name = 'table1'
0
 

Author Comment

by:Kalvyn
ID: 12169564
This is just ust what i was looking for

select sysobjects.name, syscolumns.name, value
FROM (sysobjects INNER JOIN syscolumns
ON syscolumns.id = sysobjects.id) LEFT JOIN sysproperties
ON sysproperties.smallid = syscolumns.colid AND
sysproperties.id = syscolumns.id
left join INFORMATION_Schema.columns c
on sysproperties.name=c.COLUMN_NAME
where sysproperties.name='MS_Description'
and sysobjects.name = 'control'

--returns

Control      System      this is the system description
Control      topic      THis is the topic description

thanks all who participated!
James
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

724 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