我有这样的XML,存储在sql server的XML字段中:
<TableSpec
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="custom_schema_name"
ID="66912703-1201-457C-A37B-84D446B6C043"
Name="Some name"
>
<Fields>
<ForeignKeyField Name="SOMETABLEID" Required="true" ForeignTable="SOMETABLE"/>
<GuidField Name="FIELD1" Required="true" />
<DateField Name="FIELD2" Required="true" />
</Fields>
</TableSpec>
我想选择不外键字段的每个字段的“名称”。
鉴于此示例,我希望我的结果集为:
FIELD1
FIELD2
我该怎么做?
答案 0 :(得分:1)
这是一些伪代码。您必须为特定样本修改此项。
编辑:我修改了代码以反映您的命名空间。
declare @x xml
set @x =
'<TableSpec
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="custom_schema_name"
ID="66912703-1201-457C-A37B-84D446B6C043"
Name="Some name">
<Fields>
<ForeignKeyField Name="SOMETABLEID" Required="true" ForeignTable="SOMETABLE"/>
<GuidField Name="FIELD1" Required="true" />
<DateField Name="FIELD2" Required="true" />
</Fields>
</TableSpec>'
;WITH XMLNAMESPACES ('custom_schema_name' as custom_schema_name)
select
x.i.value('@Name','varchar(256)'),
x.i.value('local-name(.)','varchar(256)')
from @x.nodes('/custom_schema_name:TableSpec/custom_schema_name:Fields/*') x(i)
where x.i.value('local-name(.)','varchar(256)') <> 'ForeignKeyField'