Solved

sql syntax to list column names and the description

Posted on 2004-08-27
7
735 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
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.
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.

732 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