SQL - 取得 Database 的資料表資訊

摘要:SQL - 取得 Database 的資料表資訊

在前一篇<SQL - 取得 Database 的預存程序資訊>中查找到資料庫的預存程序相關資訊,本篇就來試著抓取資料庫裡的資料表資訊了,以下就來實做唄...

T-SQL:

SELECT A.[NAME] AS '結構敘述',
B.[NAME] AS '資料表名稱',
C.[COLUMN_ID] AS '欄位順序',
C.[NAME] AS '欄位名稱',
D.[NAME] AS '傳入參數型別',
C.[MAX_LENGTH] AS '長度',
C.[PRECISION] AS '精確度',
C.[SCALE] AS '刻度',
C.[IS_NULLABLE] AS '是否為NULL',
B.[CREATE_DATE] AS '建立時間',
B.[MODIFY_DATE] AS '修改時間'
FROM SYS.SCHEMAS A INNER JOIN SYS.TABLES B
ON A.[SCHEMA_ID] = B.[SCHEMA_ID]
INNER JOIN SYS.COLUMNS C
ON B.[OBJECT_ID] = C.[OBJECT_ID]
INNER JOIN SYS.TYPES D
ON C.[SYSTEM_TYPE_ID] = D.[SYSTEM_TYPE_ID] AND C.[USER_TYPE_ID] = D.[USER_TYPE_ID]
WHERE B.[TYPE] = 'U'
ORDER BY A.[NAME] ASC

結果:

以北風資料庫為例