如何检查表中的列是否具有特定的数据类型?
例如,如何检查SQL Server表中的列是否属于数据类型char(11)
?
select COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS
where DATA_TYPE = 'char'
and CHARACTER_MAXIMUM_LENGTH = 11
and TABLE_NAME = 'your_table'
使用syscolumns
:
SELECT name FROM SYSCOLUMNS
where length = 11
and xtype = 175 --char type
select case when DATA_TYPE= 'char' then 'T' else 'F' end,
case when CHARACTER_MAXIMUM_LENGTH = 11 then 'T' else 'F' end
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME = 'MY_COLUMN_NAME'
and TABLE_NAME = 'MY_TABLE_NAME'
查询列数据类型的sql查询
ALTER TABLE table_name MODIFY COLUMN column_name data_type;