Solved

sql syntax to list column names and the description

Posted on 2004-08-27
7
724 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 69

Expert Comment

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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 INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now