如何用 SQL 确定小数类型列的小数位数
How determine the scale of a decimal type column with SQL
我需要将精度为 16、小数位数为 2 的小数类型列更改为精度为 16、小数位数为 5,对此我将执行以下操作:
ALTER TABLE dbo.my_table ALTER COLUMN my_column DECIMAL(16, 5)
但是为了避免每次应用程序运行时都进行此更改,我想读取列比例,如果它不同于 5,则会执行上面的行。
有没有办法获取小数类型列的比例?
Is there a way to get the scale of a column of decimal type?
可以查询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'
您可以从 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';
我需要将精度为 16、小数位数为 2 的小数类型列更改为精度为 16、小数位数为 5,对此我将执行以下操作:
ALTER TABLE dbo.my_table ALTER COLUMN my_column DECIMAL(16, 5)
但是为了避免每次应用程序运行时都进行此更改,我想读取列比例,如果它不同于 5,则会执行上面的行。
有没有办法获取小数类型列的比例?
Is there a way to get the scale of a column of decimal type?
可以查询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'
您可以从 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';