无法将 NULL 插入 SQL CE 中的 NTEXT 字段?
一个简单的例子:
CREATE TABLE People (
personID int not null
, name nvarchar(50) not null
, addrLine1 nvarchar(50) null
, addrLine2 nvarchar(50) null
, suburb nvarchar(30) not null
, xmlRawInput ntext not null
, xmlRawOutput ntext null
)
GO
INSERT People (
personID
, name
, addrLine1
, addrLine2
, suburb
, xmlRawInput
, xmlRawOutput
)
SELECT
101 AS personID
, 'george benson' AS name
, '123 help st' AS addrLine1
, NULL AS addrLine2
, 'los angeles' AS suburb
, '<example>record<example>' AS xmlRawInput
, 'I LOVE MICROSOFT' AS xmlRawOutput
GO
这很好用;注意 Null 可以很容易地插入到 addrLine2 列中;但如果我改变 在 ntext 列上将“我爱微软”设置为 NULL,我收到以下错误: 不支持转换。 [要转换的类型(如果已知)= int,要转换为的类型(如果已知)= ntext]
以下插入失败;有什么想法吗?我使用的是CE版本3.5.8080.0。
INSERT People (
personID
, name
, addrLine1
, addrLine2
, suburb
, xmlRawInput
, xmlRawOutput
)
SELECT
101 AS personID
, 'george benson' AS name
, '123 help st' AS addrLine1
, NULL AS addrLine2
, 'los angeles' AS suburb
, '<example>record<example>' AS xmlRawInput
, NULL AS xmlRawOutput
GO
A simple example:
CREATE TABLE People (
personID int not null
, name nvarchar(50) not null
, addrLine1 nvarchar(50) null
, addrLine2 nvarchar(50) null
, suburb nvarchar(30) not null
, xmlRawInput ntext not null
, xmlRawOutput ntext null
)
GO
INSERT People (
personID
, name
, addrLine1
, addrLine2
, suburb
, xmlRawInput
, xmlRawOutput
)
SELECT
101 AS personID
, 'george benson' AS name
, '123 help st' AS addrLine1
, NULL AS addrLine2
, 'los angeles' AS suburb
, '<example>record<example>' AS xmlRawInput
, 'I LOVE MICROSOFT' AS xmlRawOutput
GO
This works fine; Notice Null can easily be inserted into addrLine2 column; But if i change
'I Love Microsoft' to NULL on the ntext column, i get following error:
The conversion is not supported. [ Type to convert from (if known) = int, Type to convert to (if known) = ntext ]
Insert fails for below; any ideas? I am using CE version 3.5.8080.0.
INSERT People (
personID
, name
, addrLine1
, addrLine2
, suburb
, xmlRawInput
, xmlRawOutput
)
SELECT
101 AS personID
, 'george benson' AS name
, '123 help st' AS addrLine1
, NULL AS addrLine2
, 'los angeles' AS suburb
, '<example>record<example>' AS xmlRawInput
, NULL AS xmlRawOutput
GO
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以尝试的两个想法:
在
insert
中使用values
子句而不是select
子句,即:如果出于某种原因,您必须使用
select
子句(为什么?),尝试将NULL
显式转换为ntext
。即,选择convert(ntext, NULL)
。如果
values
子句有效,则原因可能是 SqlCE 引擎很愚蠢,并且在select
语句中为null
使用了错误的默认数据类型。convert
可能有效,也可能无效,具体取决于null
首先采用的数据类型,因为从技术上讲,您无法将任何内容转换为ntext
..但值得尝试。Two ideas to try:
Use a
values
clause instead ofselect
clause in yourinsert
, ie:If, for some reason, you must use the
select
clause (why?), try casting theNULL
tontext
explicitly. ie,select convert(ntext, NULL)
.If the
values
clause works, then the cause is probably the SqlCE engine being dumb and using the wrong default datatype for thenull
in theselect
statement. Theconvert
may or may not work, depending on what datatypenull
takes on first, since technically you can't cast anything tontext
... but it's worth trying.