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?
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%';
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With