سؤال

I have a ProductInventory table with over 100 fields. Many need to offer user guidance to understand. I’m looking for a way to add tooltips to the form without hardcoding the text.

I want to use MS SQL table field property DESCRIPTION as the source for user tooltips during web forms data entry.

Generally, my descriptions are for other db admins, but I was wondering if with a little more thoughtful and friendly descriptions, I could make duel use of this extended SQL field property.

Is is possible to retrieve this field property WITH a datatable/dataset query?

Example:

FieldName: ProductID Value: [string] FieldDescription: “This is a description for the end user of what the ProductID is field used for”

I know we can get the field schema for a specific field by…

SELECT @Result = count(1) 
FROM   ::fn_listextendedproperty (N'MS_Description',  N'Schema',  'dbo', 
N'Table',   '[Your Table Name]', 
N'Column',  '[You Field Name]')

Is this possible without a “per field” query on each field?

Perhaps run two queries. One to pull records and one to pull schema. (i.e. retrieve entire table schema and do a loop to find matching field name.)

Any thoughts?

هل كانت مفيدة؟

المحلول

You can get those descriptions back via query for the whole table.

This is almost entirely lifted from Phil Factor on SQL Server Central. My modifications are only the extra join condition p.name = 'MS_Description' and the where clause.

SELECT SCHEMA_NAME(tbl.schema_id) AS [Table_Schema],
       tbl.name AS [Table_Name],
       clmns.name AS [Column_Name],
       p.name AS [Name],
       CAST(p.value AS SQL_VARIANT) AS [Value]
  FROM sys.tables AS tbl
       INNER JOIN sys.all_columns AS clmns ON clmns.OBJECT_ID=tbl.OBJECT_ID
       INNER JOIN sys.extended_properties AS p ON p.major_id=clmns.OBJECT_ID
                                              AND p.minor_id=clmns.column_id
                                              AND p.class= 1
                                              AND p.name = 'MS_Description'
 WHERE tbl.name = 'Your Table Name'                                       
ORDER BY [Table_Schema] ASC,
         [Table_Name] ASC,
         [Column_ID] ASC,
         [Name] ASC

نصائح أخرى

try

  select    
    s.name [Schema], 
    ao.[name] [Table],
    ac.[name] [Column],
    t.[name] [Type],
    p.[value] [Description]
    from sys.all_columns ac
    inner join sys.types t on ac.system_type_id = t.system_type_id
    inner join sys.all_objects ao on  ao.object_id = ac.object_id
    inner join sys.schemas s on s.schema_id = ao.schema_id
    LEFT OUTER JOIN  sys.extended_properties p ON  p.major_id = ac.object_id  AND p.minor_id = ac.column_id
    where ao.type = 'u'
    order by s.name, ao.name, ac.name

with ao.type = 'u' (all user tables)

if you want only one table use ao.name = 'table_name'

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top