基於SQL2005 SQL2008 表結構資訊查詢升級版的詳解(含外鍵資訊)_mssql2005

來源:互聯網
上載者:User
SELECT
表名=CASE WHEN a.colorder = 1 THEN d.name ELSE '' END,
表說明=CASE WHEN a.colorder = 1 THEN isnull(f.value, '') ELSE '' END ,
欄位序號=a.colorder ,
欄位名=a.name ,
標識=CASE WHEN COLUMNPROPERTY(a.id, a.name, 'IsIdentity') = 1 THEN '√' ELSE '' END ,
主鍵=CASE WHEN EXISTS (
  SELECT 1
  FROM dbo.sysindexes si
  INNER JOIN dbo.sysindexkeys sik ON si.id = sik.id AND si.indid = sik.indid
  INNER JOIN dbo.syscolumns sc ON sc.id = sik.id AND sc.colid = sik.colid
  INNER JOIN dbo.sysobjects so ON so.name = so.name AND so.xtype = 'PK'
  WHERE sc.id = a.id AND sc.colid = a.colid) THEN '√'
  ELSE ''
 END ,
 外鍵=CASE WHEN tony.fkey is not null and tony.fkey=a.colid THEN '√' ELSE '' END ,
 外鍵表=CASE WHEN tony.fkey is not null and tony.fkey=a.colid
THEN object_name(tony.fkeyid) ELSE ''
   END ,
 外鍵欄位=CASE WHEN tony.fkey is not null and tony.fkey=a.colid
 THEN (SELECT name FROM syscolumns
   WHERE colid=tony.fkey AND id=tony.fkeyid)  
 ELSE ''
 END,
 類型=b.name ,
 長度=a.length ,
 精度=COLUMNPROPERTY(a.id, a.name, 'PRECISION') ,
 小數位元=ISNULL(COLUMNPROPERTY(a.id, a.name, 'Scale'), 0) ,
 允許空=CASE WHEN a.isnullable = 1 THEN '√' ELSE '' END ,
 預設值= ISNULL(e.text, ''),
 欄位說明=ISNULL(g.[value], ''),
 建立時間=d.crdate,
 更改時間=CASE WHEN a.colorder = 1 THEN d.refdate ELSE NULL END
FROM dbo.syscolumns a
LEFT JOIN dbo.systypes b ON a.xtype = b.xusertype
INNER JOIN dbo.sysobjects d ON a.id = d.id AND d.xtype = 'U' AND d.status >= 0
LEFT JOIN dbo.syscomments e ON a.cdefault = e.id
LEFT JOIN sys.extended_properties g ON a.id = g.major_id AND a.colid = g.minor_id
LEFT JOIN sys.extended_properties f ON d.id = f.major_id AND f.minor_id = 0 
LEFT JOIN sysobjects htl ON htl.parent_obj=d.id AND htl.xtype='F'
LEFT JOIN sysforeignkeys tony on htl.id=tony.constid
WHERE d.name='tb'  --這裡輸入包含表名稱的條件
ORDER BY d.id, a.colorder

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.