xml数据在数据库表中显示为NULL。为什么?

时间:2013-09-04 09:53:47

标签: sql sql-server sql-server-2008 xml-serialization sqlxml

我使用下面的存储过程将xml数据保存到数据库表中但在表中值显示为NULL。

什么是问题,请帮助我。

 ALTER PROCEDURE [dbo].[prc_readxmldata2]
(
@XMLdata XML
)
AS
BEGIN

declare @hDoc int

exec sp_xml_preparedocument @hDoc OUTPUT,@XMLdata

insert into xmlTable (Name, Value)
select xml.Name, xml.Value
from OPENXML(@hDoc,'/UpgradeLog/Properties/Property',1)
with(Name varchar(50) 'text()',
Value varchar(50) 'text()')xml

exec sp_xml_removedocument @hDoc

以下是我的XML文件。

    <?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='_UpgradeReport_Files/UpgradeReport.xslt'?><UpgradeLog>
<Properties>
<Property Name="Solution" Value="ecs_myecsinc">
</Property>
<Property Name="Solution File" Value="D:\Working Projects\ECSInc\ECSInc\ECSInc\ecs_myecsinc.sln">
</Property>
<Property Name="Date" Value="Wednesday, July 27, 2011">
</Property>
<Property Name="Time" Value="20:56 PM">
</Property>
</Properties>
<Event ErrorLevel="0" Project="" Source="ecs_myecsinc.sln" Description="File successfully backed up as D:\Working Projects\ECSInc\ECSInc\ECSInc\Backup\ecs_myecsinc.sln">
</Event>
<Event ErrorLevel="0" Project="" Source="ecs_myecsinc.sln" Description="Solution converted successfully">
</Event>
<Event ErrorLevel="3" Project="" Source="ecs_myecsinc.sln" Description="Converted">
</Event>
</UpgradeLog>

1 个答案:

答案 0 :(得分:1)

select xml.Name, xml.Value
from OPENXML(@hDoc,'/UpgradeLog/Properties/Property',1)
with(
    Name varchar(50) '@Name',
    Value varchar(50) '@Value'
)

或者你可以使用SQLXML从@XMLData选择:

select
    T.C.value('@Name', 'nvarchar(50)') as Name,
    T.C.value('@Value', 'nvarchar(50)') as Value
from @XMLdata.nodes('/UpgradeLog/Properties/Property') as T(C)
两种解决方案都

sql fiddle demo