查询数据字典.sql 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. --按照Schema查询数据字典
  2. select 表名=case when colorder = -1 then name else '' end,
  3. 表说明=case when colorder = -1 then isnull(fvalue, '') else '' end,
  4. 字段名,
  5. 字段说明,
  6. 类型,
  7. 长度,
  8. 主键,
  9. 允许空,
  10. 默认值
  11. from (
  12. SELECT d.name,
  13. f.value as fvalue,
  14. 字段名=a.name,
  15. 字段说明=isnull(g.[value], ''),
  16. 类型=b.name,
  17. 长度=COLUMNPROPERTY(a.id, a.name, 'PRECISION'),
  18. 主键=case
  19. when exists(SELECT 1
  20. FROM sysobjects
  21. where xtype = 'PK'
  22. and name in (
  23. SELECT name
  24. FROM sysindexes
  25. WHERE indid in (
  26. SELECT indid
  27. FROM sysindexkeys
  28. WHERE id = a.id AND colid = a.colid
  29. ))) then '√'
  30. else '' end,
  31. 允许空=case when a.isnullable = 1 then '√' else '×' end,
  32. 默认值=isnull(e.text, ''),
  33. a.id,
  34. a.colorder
  35. FROM syscolumns a
  36. left join systypes b on a.xtype = b.xusertype
  37. inner join sysobjects d on a.id = d.id and d.xtype = 'U' and d.name <> 'dtproperties'
  38. left join syscomments e on a.cdefault = e.id
  39. left join sys.extended_properties g on a.id = g.major_id and a.colid = g.minor_id
  40. left join sys.extended_properties f on d.id = f.major_id and f.minor_id = 0
  41. left join sys.tables t on t.object_id = d.id
  42. left join sys.schemas s on s.schema_id = t.schema_id
  43. where s.name = 'SL_pc_Schema'
  44. union all
  45. select d.name,
  46. isnull(f.value, ''),
  47. null,
  48. null,
  49. null,
  50. null,
  51. null,
  52. null,
  53. null,
  54. -1,
  55. -1
  56. from sysobjects d
  57. left join sys.extended_properties f on d.id = f.major_id and f.minor_id = 0
  58. left join sys.tables t on t.object_id = d.id
  59. left join sys.schemas s on s.schema_id = t.schema_id
  60. where s.name = 'SL_pc_Schema'
  61. ) as tmp
  62. order by name, id, colorder