sql server—如何使用sql确定十进制类型列的小数位数

lsmepo6l  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(264)

我需要将精度为16且小数位数为2的十进制类型列更改为精度为16且小数位数为5,为此我将执行以下操作:

ALTER TABLE dbo.my_table ALTER COLUMN my_column DECIMAL(16, 5)

但是为了避免每次应用程序运行时都进行这种更改,我想读取列比例,如果它与5不同,则执行上面的行。
有没有办法得到十进制类型列的小数位数?

bz4sfanl

bz4sfanl1#

有没有办法得到十进制类型列的小数位数?
您可以查询 information_schema.columns :

select column_name, numeric_precision, numeric_scale 
from information_schema.columns
where table_schema = 'dbo' and table_name = 'my_table' and column_name = 'my_column'
emeijp43

emeijp432#

你可以从 sys 物体:

SELECT ct.name AS DataType,
       c.precision,
       c.scale
FROM sys.schemas s
     JOIN sys.tables t ON s.schema_id = t.schema_id
     JOIN sys.columns c ON t.object_id = c.object_id
     JOIN sys.types ct ON c.system_type_id = ct.system_type_id
WHERE s.name = N'Your Schema'
  AND t.name = N'Your Table'
  AND c.name = N'Your Column';

相关问题