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

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

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

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

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

bz4sfanl

bz4sfanl1#

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

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

emeijp432#

你可以从 sys 物体:

  1. SELECT ct.name AS DataType,
  2. c.precision,
  3. c.scale
  4. FROM sys.schemas s
  5. JOIN sys.tables t ON s.schema_id = t.schema_id
  6. JOIN sys.columns c ON t.object_id = c.object_id
  7. JOIN sys.types ct ON c.system_type_id = ct.system_type_id
  8. WHERE s.name = N'Your Schema'
  9. AND t.name = N'Your Table'
  10. AND c.name = N'Your Column';

相关问题