我正在SQL Server中的另一个表中选择并插入数据,并得到以下错误:
将货币转换为数据类型数字时发生算术溢出错误。
如何识别导致此问题的数据?
CREATE TABLE foo
(
myNum DECIMAL(10, 4)
);
INSERT INTO foo
SELECT 1234.567;
您可以使用try_convert()
:
select f.*
from foo f
where try_convert(decimal(10, 4), col) is null;
CREATE TABLE #tmp_1(col1 DECIMAL(2,0))
CREATE TABLE #tmp_2(col2 DECIMAL(12,0))
CREATE TABLE #errors (error_value VARCHAR(MAX))
INSERT INTO #tmp_2 VALUES (1222.00)
INSERT INTO #tmp_2 VALUES (122222.00)
INSERT INTO #tmp_2 VALUES (1.00)
DECLARE @rec DECIMAL(12,0)
DECLARE get_errors CURSOR FOR SELECT col2 FROM #tmp_2
OPEN get_errors
FETCH NEXT FROM get_errors INTO @rec
WHILE @@fetch_status <> -1
BEGIN
BEGIN TRY
INSERT #tmp_1 VALUES (@rec)
END TRY
BEGIN CATCH
INSERT INTO #errors VALUES (@rec)
END CATCH
FETCH NEXT FROM get_errors INTO @rec
END
CLOSE get_errors
DEALLOCATE get_errors
GO
SELECT * FROM #tmp_1
SELECT * FROM #errors
DROP TABLE #tmp_1
DROP TABLE #tmp_2
DROP TABLE #errors