1

我正在使用下面的存储过程将 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>
4

1 回答 1

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

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

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两种解决方案。

于 2013-09-04T10:07:22.900 回答