Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching column description description meta across all tables

Tags:

sql-server

I am considering using extended properties to store table and column descriptions but I want to be able to search the descriptions across for ALL tables looking for the occurrence of a particular substring.

In the following example, the function shown returns a list the column descriptions for the table "PEOPLE." These descriptions are stored as as extended properties with name "MS_DESCRIPTION."

   SELECT
    cast(VALUE AS VARCHAR(8000)) AS [DESCRIPTION]
                     FROM
                         ::fn_listextendedproperty(NULL
                                                   ,'user'
                                                   ,'dbo'
                                                   ,'table'
                                                   ,'PEOPLE'
                                                   ,'column'
                                                   ,NULL)
                                           

However, how do I search all column descriptions across all tables for a given substring?

SELECT
    cast(VALUE AS VARCHAR(8000)) AS [DESCRIPTION]
                     FROM
                         ::fn_listextendedproperty(NULL
                                                   ,'user'
                                                   ,'dbo'
                                                   ,'table'
                                                   ,'?'
                                                   ,'column'
                                                   ,NULL)
    where cast(VALUE AS VARCHAR(8000)) LIKE '%SEARCH%'

Is there a more efficient way to search that would avoid a conversion of the VALUE field from a SQL_VARIANT to a varchar?

Would it be wiser to store this meta data in user defined custom tables instead of using extended properties?

like image 823
Chad Avatar asked Dec 04 '25 05:12

Chad


2 Answers

You can look at the system views to search for all columns across all tables.

SELECT schemas.name schemaName
     , tables.name tableName
     , columns.name columnName
     , extended_properties.value extendedProperties
  FROM sys.schemas
 INNER JOIN sys.tables
    ON schemas.schema_id = tables.schema_id
 INNER JOIN sys.columns
    ON tables.object_id = columns.object_id
 INNER JOIN sys.extended_properties
    ON tables.object_id = extended_properties.major_id
   AND columns.column_id = extended_properties.minor_id
   AND extended_properties.name = 'MS_Description'
   AND CAST( extended_properties.value AS nvarchar(max) ) LIKE '%SEARCH%';
like image 200
Drew Leffelman Avatar answered Dec 06 '25 23:12

Drew Leffelman


To find columns of a certain name, user1948904's solution gets a little simpler:

SELECT schemas.name schemaName
   , tables.name tableName
   , columns.name columnName
   , * -- for context if you like
 FROM sys.schemas
 INNER JOIN sys.tables
   ON schemas.schema_id = tables.schema_id
 INNER JOIN sys.columns
   ON tables.object_id = columns.object_id
 WHERE columns.name LIKE '%SEARCH%'

The search is case insensitive.

like image 41
veeTrain Avatar answered Dec 06 '25 23:12

veeTrain