无法将 NULL 插入 SQL CE 中的 NTEXT 字段?

发布于 2024-10-05 12:16:57 字数 1521 浏览 3 评论 0原文

一个简单的例子:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨光如昨 2024-10-12 12:16:57

可以尝试的两个想法:

insert 中使用 values 子句而不是 select 子句,即:

INSERT People (
      personID          
    , name              
    , addrLine1         
    , addrLine2         
    , suburb            
    , xmlRawInput       
    , xmlRawOutput
)
VALUES (
      101
    , 'george benson'
    , '123 help st'
    , NULL     
    , 'los angeles'
    , '<example>record<example>'
    , NULL 
)

如果出于某种原因,您必须使用select 子句(为什么?),尝试将 NULL 显式转换为 ntext。即,选择convert(ntext, NULL)

如果 values 子句有效,则原因可能是 SqlCE 引擎很愚蠢,并且在 select 语句中为 null 使用了错误的默认数据类型。 convert 可能有效,也可能无效,具体取决于 null 首先采用的数据类型,因为从技术上讲,您无法将任何内容转换为 ntext ..但值得尝试。

Two ideas to try:

Use a values clause instead of select clause in your insert, ie:

INSERT People (
      personID          
    , name              
    , addrLine1         
    , addrLine2         
    , suburb            
    , xmlRawInput       
    , xmlRawOutput
)
VALUES (
      101
    , 'george benson'
    , '123 help st'
    , NULL     
    , 'los angeles'
    , '<example>record<example>'
    , NULL 
)

If, for some reason, you must use the select clause (why?), try casting the NULL to ntext 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 the null in the select statement. The convert may or may not work, depending on what datatype null takes on first, since technically you can't cast anything to ntext ... but it's worth trying.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文