0

我正在尝试将数据从 SQL Server 插入到链接服务器,但遇到以下问题:

Msg 8114, Level 16, State 11, Line 1
Error converting data type nvarchar to (null).

我的查询是:

INSERT piserver.piarchive..picomp2 
(tag, time, value, annotations)
SELECT cast(c.name as nvarchar) as name, convert(datetime, c.started_at) as started_at, c.value, cast(c.log as nvarchar) as log
FROM [database].[dbo].[table1] c
WHERE c.id = 206

前 3 列中没有空值,但即使我从查询中删除最后一列,我仍然遇到空错误。

picomp2 的数据类型有: tag (WString(4000), not null) time (DateTime, not null) value (Variant, null) annotations (Variant, null)

table1 的数据类型都是 varchar,但 value 是 float。

4

1 回答 1

0

在 SQL Server 中,您总是希望包含一个长度。默认值因上下文而异。目前尚不清楚长度应该是多少,但您可能只是依靠隐式转换来做正确的事情:

INSERT piserver.piarchive..picomp2 (tag, time, value, annotations)
    SELECT c.name, try_convert(datetime, c.started_at) as started_at, c.value, c.log 
    FROM [database].[dbo].[table1] c
    WHERE c.id = 206;

我还建议使用try_convert()for datetime,除非您希望转换错误停止INSERT.

于 2021-02-23T15:00:13.020 回答