(MSSQL)查詢table/view欄位DATATYPE

透過SYS.COLUMNS JOIN SYS.TYPES查詢

1
2
3
4
5
6
7
8
9
10
SELECT c.name,
t.name,
c.max_length,
c.precision,
c.scale
FROM SYS.COLUMNS c
JOIN SYS.TYPES t
ON t.user_type_id = c.user_type_id
AND t.system_type_id = c.system_type_id
WHERE object_id = OBJECT_ID('TABLENAME/VIEWNAME')

透過INFORMATION_SCHEMA.COLUMNS查詢

1
2
3
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TABLENAME/VIEWNAME'