Translate

Thursday 30 October 2014

Using SQL Server Metadata and Statistics to Build a Table

select o.name as [Name], sc.name as [Type], s.row_count as [Size]
 from sys.objects o        
left join sys.schemas sc on sc.schema_id = o.schema_id
left join (select object_id, sum(rows) as row_count from sys.partitions 
      where index_id < 2
    group by object_id) s on o.object_id = s.object_id
where o.type = 'U'
order by sc.name, o.name

Extract Table Meta Data (description, fields and their data types)


SELECT   u.name + '.' + t.name AS [table],
            td.value AS [table_desc],
      c.name AS [column],
      cd.value AS [column_desc]
FROM     sysobjects t
INNER JOIN  sysusers u
    ON  u.uid = t.uid
LEFT OUTER JOIN sys.extended_properties td
    ON  td.major_id = t.id
    AND  td.minor_id = 0
    AND  td.name = 'MS_Description'
INNER JOIN  syscolumns c
    ON  c.id = t.id
LEFT OUTER JOIN sys.extended_properties cd
    ON  cd.major_id = c.id
    AND  cd.minor_id = c.colid
    AND  cd.name = 'MS_Description'
WHERE t.type = 'u'
ORDER BY    t.name, c.colorder