SQL Server 2014 匯出資料字典

李晉江發表於2020-11-21

SQL Server 2014 匯出資料字典

系統:Win10
資料庫:SQL Server 2014

需求描述

SQL Server 2014 需要匯出 repair_system 資料庫的資料字典

操作步驟

  1. 開啟 SQL Server 2014 Management Studio
  2. 新建查詢
  3. 選擇 repair_system(根據需求選擇自己的資料庫)資料庫
  4. 執行下面的 SQL 語句
  5. 可以看到該資料庫下的表單及欄位的詳細資訊都已經查詢出來了
--快速檢視資料庫的表結構
SELECT  CASE WHEN col.colorder = 1 THEN obj.name
                  ELSE ''
             END AS 表名,
        col.colorder AS 序號 ,
        col.name AS 列名 ,
        ISNULL(ep.[value], '') AS 列說明 ,
        t.name AS 資料型別 ,
        col.length AS 長度 ,
        ISNULL(COLUMNPROPERTY(col.id, col.name, 'Scale'), 0) AS 小數位數 ,
        CASE WHEN COLUMNPROPERTY(col.id, col.name, 'IsIdentity') = 1 THEN '√'
             ELSE ''
        END AS 標識 ,
        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 = si.name
                                                              AND so.xtype = 'PK'
                           WHERE    sc.id = col.id
                                    AND sc.colid = col.colid ) THEN '√'
             ELSE ''
        END AS 主鍵 ,
        CASE WHEN col.isnullable = 1 THEN '√'
             ELSE ''
        END AS 允許空 ,
        ISNULL(comm.text, '') AS 預設值
FROM    dbo.syscolumns col
        LEFT  JOIN dbo.systypes t ON col.xtype = t.xusertype
        inner JOIN dbo.sysobjects obj ON col.id = obj.id
                                         AND obj.xtype = 'U'
                                         AND obj.status >= 0
        LEFT  JOIN dbo.syscomments comm ON col.cdefault = comm.id
        LEFT  JOIN sys.extended_properties ep ON col.id = ep.major_id
                                                      AND col.colid = ep.minor_id
                                                      AND ep.name = 'MS_Description'
        LEFT  JOIN sys.extended_properties epTwo ON obj.id = epTwo.major_id
                                                         AND epTwo.minor_id = 0
                                                         AND epTwo.name = 'MS_Description';

在這裡插入圖片描述
如果只需要列出單獨一個表的表結構,只需要在最後面加一個條件語句即可

--快速檢視固定表的表結構
SELECT  CASE WHEN col.colorder = 1 THEN obj.name
                  ELSE ''
             END AS 表名,
        col.colorder AS 序號 ,
        col.name AS 列名 ,
        ISNULL(ep.[value], '') AS 列說明 ,
        t.name AS 資料型別 ,
        col.length AS 長度 ,
        ISNULL(COLUMNPROPERTY(col.id, col.name, 'Scale'), 0) AS 小數位數 ,
        CASE WHEN COLUMNPROPERTY(col.id, col.name, 'IsIdentity') = 1 THEN '√'
             ELSE ''
        END AS 標識 ,
        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 = si.name
                                                              AND so.xtype = 'PK'
                           WHERE    sc.id = col.id
                                    AND sc.colid = col.colid ) THEN '√'
             ELSE ''
        END AS 主鍵 ,
        CASE WHEN col.isnullable = 1 THEN '√'
             ELSE ''
        END AS 允許空 ,
        ISNULL(comm.text, '') AS 預設值
FROM    dbo.syscolumns col
        LEFT  JOIN dbo.systypes t ON col.xtype = t.xusertype
        inner JOIN dbo.sysobjects obj ON col.id = obj.id
                                         AND obj.xtype = 'U'
                                         AND obj.status >= 0
        LEFT  JOIN dbo.syscomments comm ON col.cdefault = comm.id
        LEFT  JOIN sys.extended_properties ep ON col.id = ep.major_id
                                                      AND col.colid = ep.minor_id
                                                      AND ep.name = 'MS_Description'
        LEFT  JOIN sys.extended_properties epTwo ON obj.id = epTwo.major_id
                                                         AND epTwo.minor_id = 0
                                                         AND epTwo.name = 'MS_Description'
WHERE obj.name = 'order' --表名 ;

在這裡插入圖片描述

相關文章