[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

sql syntax to list column names and the description

Posted on 2004-08-27
7
Medium Priority
?
742 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 70

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 1000 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
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
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

649 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