我需要一种智能方法,以可在 CREATE TABLE 语句中使用的方式从 INFORMATION_SCHEMA.COLUMNS 中获取数据类型。问题是需要理解“额外”字段,例如 NUMERIC_
PRECISION 和 NUMERIC_
SCALE。
显然,我可以忽略 INTEGER 列(精度为 10,小数位数为 0),但我还会对其他类型感兴趣,例如 NUMERIC。因此,无需编写大量代码来解析表,关于如何从列定义中获取某种字段速记有什么想法吗?
我希望能够得到类似的东西: 整数, 约会时间, 钱, 数字**(10,2)**
最佳答案
select column_type = data_type +
case
when data_type like '%text' then ''
when data_type like '%char' and character_maximum_length = -1 then '(max)'
when character_maximum_length is not null then '(' + convert(varchar(10), character_maximum_length) + ')'
when data_type = 'numeric' then '(' + convert(varchar(10), isnull(numeric_precision, 18)) + ', ' +
convert(varchar(10), isnull(numeric_scale, 0)) + ')'
else ''
end
,*
from information_schema.columns
关于sql-server - 如何获得带有括号和所有内容的 SQL Server 列定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/253324/