Kev*_*cht 6 xml t-sql sql-server null openxml
我有以下(高度简化的)XML文档,我使用OPENXML函数读入我的数据库:
<root>
<row>
<bar>123</bar>
</row>
<row>
<bar>0</bar>
</row>
<row>
<bar></bar>
</row>
</root>
Run Code Online (Sandbox Code Playgroud)
我像这样导入数据库:
insert into [Foo]
([bar])
select
ds.[bar]
from openxml(@xmlHandle, 'root/row', 2)
with ([bar] int) ds
Run Code Online (Sandbox Code Playgroud)
问题是OPENXML将int数据类型的空字段转换为零,因此这将插入到我的表中:
bar
----
123
0
0
Run Code Online (Sandbox Code Playgroud)
我想要插入到我的表中的是:
bar
----
123
0
NULL
Run Code Online (Sandbox Code Playgroud)
如何让OPENXML函数将空字段视为NULL并且默认情况下不将其转换为零?
刚刚遇到类似的问题并用NULLIFSQL中的函数解决了它.
我相信你也会忽略它:)
insert into [Foo]
([bar])
select
NULLIF(ds.[bar], '')
from openxml(@xmlHandle, 'root/row', 2)
with ([bar] nvarchar(20)) ds
Run Code Online (Sandbox Code Playgroud)
摆脱CASE... END语句创建的混乱.
希望能帮助到你!
既然没有人有任何想法,这就是我如何"解决"它,虽然这对我来说似乎是一个黑客攻击:
insert into [Foo]
([bar])
select
isnull(ds.[bar], '') when '' then null else CAST(ds.[bar] as int) end
from openxml(@xmlHandle, 'root/row', 2)
with ([bar] nvarchar(20)) ds
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7496 次 |
| 最近记录: |