我有一个表要更新一个列值:
UPDATE t1
SET [Value] = t2.[Value]
FROM table1 AS t1
INNER JOIN table2 AS t2
ON (t1.ID = t2.ID)
table1(table)和table2(view)的结构相同。
当我运行以上更新时,我得到:
Msg 8115, Level 16, State 7, Line 1
Arithmetic overflow error converting numeric to data type numeric.
The statement has been terminated.
我通过运行以下查询检查了列值是否不同:
SELECT
c.name 'Column Name',
t.Name 'Data type',
c.max_length 'Max Length',
c.precision ,
c.scale ,
c.is_nullable,
ISNULL(i.is_primary_key, 0) 'Primary Key'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id = t.user_type_id
LEFT OUTER JOIN
sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
LEFT OUTER JOIN
sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
WHERE
c.object_id = OBJECT_ID('table1') --and the same for table2
获得相同的数据类型,最大长度等,结果相同。
那我为什么会收到这个错误?
我查了NUMERIC_ROUNDABORT 但不是真的喜欢它......还有其他任何解决方案吗?
答案 0 :(得分:0)
在t2.Value周围添加ROUND函数,精度为10会改变什么吗?实际精度取决于列定义。
UPDATE t1
SET [Value] = ROUND(t2.[Value], 10)
FROM table1 AS t1
INNER JOIN table2 AS t2
ON (t1.ID = t2.ID
希望看到CREATE TABLE语句。
答案 1 :(得分:0)
您应该检查数字字段的精确度。
每个数字字段通常使用两个参数定义,如:numeric(5,2)
这意味着:
a)字段值是数字 b)该领域共有5个数字 c)其中2个为小数后保留
如果您要使用numeric(5,2)
字段更新numeric(7,2)
的数字字段,则可能会导致问题。