如何用SQL确定小数类型列的比例尺

问题描述 投票:0回答:1

我需要将一个精度为16,比例为2的十进制类型的列改为精度为16,比例为5的列,为此我将执行以下操作。

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

但为了避免每次程序运行时都做这种改变 我想读取列的比例尺 如果它与5不同,上面那行就会被执行。

有什么方法可以获得小数类型的列的比例?

sql datatable decimal scale
1个回答
2
投票

有什么方法可以得到一个十进制类型列的比例?

你可以查询 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'

0
投票

你可以从以下地方获得这一信息 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';
© www.soinside.com 2019 - 2024. All rights reserved.