讀取資料表中,欄位設定的相關資訊(欄位名稱,資料型別,最大長度,欄位備註...)
SELECT
a.TABLE_NAME as 表格名稱,
b.COLUMN_NAME as 欄位名稱,
b.DATA_TYPE as 資料型別,
b.CHARACTER_MAXIMUM_LENGTH as 最大長度,
b.COLUMN_DEFAULT as 預設值,
b.IS_NULLABLE as 允許空值,
( SELECT value FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', a.TABLE_NAME, 'column', default) WHERE name='MS_Description' and objtype='COLUMN' and objname Collate Chinese_Taiwan_Stroke_CI_AS = b.COLUMN_NAME ) as 欄位描述
FROM INFORMATION_SCHEMA.TABLES a
LEFT JOIN INFORMATION_SCHEMA.COLUMNS b ON ( a.TABLE_NAME=b.TABLE_NAME )
WHERE TABLE_TYPE='BASE TABLE'
AND
--列出特定表
a.TABLE_NAME =''
AND
--列出所有資料表
a.TABLE_NAME in (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES)
ORDER BY a.TABLE_NAME, ordinal_position
Example.
select b.CHARACTER_MAXIMUM_LENGTH from INFORMATION_SCHEMA.TABLES a
left join INFORMATION_SCHEMA.COLUMNS b ON (a.TABLE_NAME=b.TABLE_NAME) where TABLE_TYPE='BASE TABLE' and a.TABLE_NAME='ABC' and b.COLUMN_NAME='Test' order by a.TABLE_NAME, ordinal_position
沒有留言:
張貼留言