Solved

sql syntax to list column names and the description

Posted on 2004-08-27
7
730 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
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 42

Expert Comment

by:EugeneZ
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 42

Accepted Solution

by:
EugeneZ 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

772 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