Aug 5, 2008

How to select the table have extended property

SELECT
  *
FROM
  sys.tables AS tbl
    INNER JOIN sys.extended_properties AS p 
    ON 
      p.major_id=tbl.object_id 
      AND 
      p.minor_id=0 
      AND 
      p.class=1

How to add the extended property on each table

  1. Rewrite the content of PROPERTY_NAME
  2. Select database
  3. Run SQL
DECLARE @TABLE_NAME VARCHAR(255)
DECLARE @SQL VARCHAR(8000)
DECLARE @PROPERTY_NAME VARCHAR(255)

SET @PROPERTY_NAME = 'Description'

DECLARE C_TABLE CURSOR FOR
SELECT
name
FROM
sysobjects
WHERE
OBJECTPROPERTY(id, N'IsUserTable') = 1
AND status >= 0
ORDER BY
name

OPEN C_TABLE
FETCH NEXT FROM C_TABLE INTO @TABLE_NAME

WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM C_TABLE INTO @TABLE_NAME
PRINT @TABLE_NAME
SET @SQL =
'
EXEC sys.sp_addextendedproperty
@name=N''' + @PROPERTY_NAME + ''',
@value=N'''' ,
@level0type=N''SCHEMA'',
@level0name=N''dbo'',
@level1type=N''TABLE'',
@level1name=N''' + @TABLE_NAME + ''''

EXEC (@SQL)
END

CLOSE C_TABLE