SQL Server 2008 - 将 XML 分解到表中需要将 UNKNOWN DATE 值保留为 NULL
我想知道是否有人可以帮助
我在 SQL Server2008 中成功地将 XML 文档导入到具有 XML 数据类型的表中,但是当我尝试将任何没有输入的 DATE 值从该表粉碎到临时表时日期作为 1900-01-01 插入到临时表中。
我是否缺少一种切割方法来阻止它执行此操作,而只需插入 NULL 即可。我可以在临时表上使用 NULLIF 并将 1900-01-01 替换为 null,但我不愿意这样做,以防存在真正的 1900-01-01 值。
我的代码看起来像这样
SELECT tab.col.value('LastDate[1]','DATE') LastARD'
FROM import.XMLCompanyDetail
CROSS APPLY
xmldata.nodes('//VN/CompanyList/Row') AS tab(col)
非常感谢
请参阅下面我正在导入的示例 XML
<?xml version="1.0" encoding="ISO-8859-1" ?>
<VN>
<CompanyList>
<Row num="1"><CoNum>7878</CoNum><CoName>ExampleName</CoName><DInc>1978-12-30</DInc><DDis></DDis></Row>
</CompanyList>
</VN>
日期 DDIS 应该为 NULL,但是当它导入到我的临时表时,它会插入 1901-01-01。
更新了代码以表明我的意思
create table staging_table
(DInc DATE NULL, LastARD DATE NULL);
with XMLCompanyDetail as
(
SELECT CAST('<?xml version="1.0" encoding="ISO-8859-1" ?>
<VN>
<CompanyList>
<Row num="1"><CoNum>7878</CoNum><CoName>ExampleName</CoName><DInc>1978-12-30</DInc><DDis></DDis></Row>
</CompanyList>
</VN>
' AS XML) AS xmldata
)
INSERT INTO Staging_Table
SELECT tab.col.value('DInc[1]','DATE') DInc,
tab.col.value('DDis[1]','DATE') LastARD
FROM XMLCompanyDetail
CROSS APPLY
xmldata.nodes('//VN/CompanyList/Row') AS tab(col)
SELECT * FROM Staging_table
drop table staging_table
I was wondering if someone could help
I've successfully been able to import an XML document to a table with an XML data type in SQL Server2008, how ever when I try and shred from that table to a staging table any DATE values without an entered date are inserted to the staging table as 1900-01-01.
Is there a cleaver way i'm missing to stop it doing this and simply insert NULL instead. I could use NULLIF on the staging table and replace 1900-01-01 with null but I’m reluctant to do this in case there are genuine 1900-01-01 values.
My code looks something like this
SELECT tab.col.value('LastDate[1]','DATE') LastARD'
FROM import.XMLCompanyDetail
CROSS APPLY
xmldata.nodes('//VN/CompanyList/Row') AS tab(col)
Many thanks
Please see below the example XML i'm importing
<?xml version="1.0" encoding="ISO-8859-1" ?>
<VN>
<CompanyList>
<Row num="1"><CoNum>7878</CoNum><CoName>ExampleName</CoName><DInc>1978-12-30</DInc><DDis></DDis></Row>
</CompanyList>
</VN>
The date DDIS should be NULL but rather when it imports to my staging table it inserts 1901-01-01 instead.
updated code to show what I mean
create table staging_table
(DInc DATE NULL, LastARD DATE NULL);
with XMLCompanyDetail as
(
SELECT CAST('<?xml version="1.0" encoding="ISO-8859-1" ?>
<VN>
<CompanyList>
<Row num="1"><CoNum>7878</CoNum><CoName>ExampleName</CoName><DInc>1978-12-30</DInc><DDis></DDis></Row>
</CompanyList>
</VN>
' AS XML) AS xmldata
)
INSERT INTO Staging_Table
SELECT tab.col.value('DInc[1]','DATE') DInc,
tab.col.value('DDis[1]','DATE') LastARD
FROM XMLCompanyDetail
CROSS APPLY
xmldata.nodes('//VN/CompanyList/Row') AS tab(col)
SELECT * FROM Staging_table
drop table staging_table
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑
更新问题后,以下内容有效,但我不确定是否有更好的方法。
Edit
Following Update to the question the following works but I'm not sure if there is a better way.
尝试改用
datetime
:等等,这会返回
null
。这意味着,在插入临时表后,此null
值将被该临时表中的默认设置覆盖。因此,如果您不希望发生此行为,请将其删除。Try to use
datetime
instead:Wait, this returns
null
. That means that upon insert to your staging table thisnull
value gets overridden by the default setting in that staging table. So remove that if you don't want this behavior to occur.