SQL Server2005中如何获取表中某几列的数据类型?

2024-11-30 00:41:44
推荐回答(2个)
回答(1):

select c.name as [col_name],t.name as [type_name] from sys.columns as c
inner join sys.types as t on c.user_type_id = t.user_type_id
where c.[object_id] = object_id('dbo.YourTable') and c.[name] = 'YourColName'
或者,直接用type_name函数,更简洁:
SELECT name as [col_name], type_name(user_type_id) as [type_name] FROM sys.columns
where [object_id] = object_id('dbo.YourTable') and [name] = 'YourColName'

回答(2):

SELECT
col.name AS 列名,
typ.name as 数据类型,
col.max_length AS 占用字节数,
col.precision AS 数字长度,
col.scale AS 小数位数,
col.is_nullable AS 是否允许非空,
col.is_identity AS 是否自增,
case when exists
( SELECT 1
FROM
sys.indexes idx
join sys.index_columns idxCol
on (idx.object_id = idxCol.object_id)
WHERE
idx.object_id = col.object_id
AND idxCol.index_column_id = col.column_id
AND idx.is_primary_key = 1
) THEN 1 ELSE 0 END AS 是否是主键
FROM
sys.columns col left join sys.types typ on (col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id)
WHERE
col.object_id =
(SELECT object_id FROM sys.tables WHERE name = ' 你的表的名字 ' )