问题:我想在'组中添加更多数据。在包含其他子字段的父XML字段内。
查询:我可以将相同的详细信息捆绑到一个字段中,以节省文件大小和导入时间。
<ParentField Field1="1" Field2="X" Field3="SomeText">
此外,我正在努力做好练习,或者我应该保留我所拥有的
在UPRN之上的某些字段(如数据)也可以进入父值。我见过这样的代码:
使用我拥有的SQL代码,我该如何修改它? (下面的片段)
USE DATABASE
SELECT
e.GroupFieldName,
(SELECT
c.ShouldBeParent AS ShouldBeParent
,c.ChildIsOkayHere AS ChildIsOkayHere
FROM TblTableC c
WHERE c.GroupFieldName = e.GroupFieldName
FOR XML PATH('LineItem'), type
)
FROM TblTableE e
JOIN
TblTableC c
ON e.GroupFieldName = c.UPRN
GROUP BY e.UPRN
FOR XML PATH('GroupFieldName') , ROOT ('SURVEYDATA')
示例(我所拥有的):
<SURVEYDATA>
<UPRN_GROUP>
<UPRN>SH1001</UPRN>
<CHILD>
<Field1>Some Text </Field1>
</CHILD>
<CHILD>
<Field1>Some Other Text </Field1>
</CHILD>
</UPRN_GROUP>
<UPRN_GROUP>
<UPRN>SH1001</UPRN>
<CHILD>
<Field1>Some Text </Field1>
</CHILD>
<CHILD>
<Field1>Some Other Text </Field1>
</CHILD>
</UPRN_GROUP>
</SURVEYDATA>
答案 0 :(得分:1)
我认为您必须自己找到解决方案,因为您提供的信息不足。阅读FOR XML PATH。
下面你会发现一堆陈述。将它们复制到一个空的查询窗口并执行。特别注意我命名列的方式。带有“@”的名称将是属性,“纯”名称将是元素。
DECLARE @tbl TABLE(id INT, Caption VARCHAR(100));
INSERT INTO @tbl VALUES(1,'Caption 1'),(2,'Caption 2'),(3,'Caption 3');
DECLARE @tblChildren TABLE(id INT,ParentId INT,Caption VARCHAR(100));
INSERt INTO @tblChildren VALUES(1,1,'Caption 1.1'),(2,1,'Caption 1.2'),(3,1,'Caption 1.3')
,(4,2,'Caption 2.1'),(5,2,'Caption 2.2')
,(6,3,'Caption 3.1');
--Simple SELECT
SELECT tbl.id AS ParentId
,tbl.Caption AS ParentCaption
,ch.id AS ChildId
,ch.Caption AS ChildCaption
FROM @tbl AS tbl
INNER JOIN @tblChildren AS ch ON tbl.id=ch.ParentId;
--XML with elements and attributes (play around with this...)
--Children are not nested...
SELECT tbl.Caption AS [Parent/@Caption]
,tbl.id AS [Parent/@Id]
,ch.Caption AS [Child/@Caption]
,ch.id AS [Child/@Id]
FROM @tbl AS tbl
INNER JOIN @tblChildren AS ch ON tbl.id=ch.ParentId
FOR XML PATH('row'),ROOT('root');
--XML with elements and attributes (play around with this...)
--Children are blocked...
SELECT tbl.Caption AS [Parent/@ParentCaption]
,tbl.id AS [Parent/@Id]
,(SELECT id AS [@id]
,Caption AS [@Caption]
FROM @tblChildren
WHERE ParentId=tbl.id
FOR XML PATH('Child'),ROOT('Children'),TYPE
)
FROM @tbl AS tbl
FOR XML PATH('row'),ROOT('root');
--XML with elements and attributes (play around with this...)
--Children are nested...
--No row-tag needed, as everything is nested
SELECT tbl.Caption AS [Parent/@ParentCaption]
,tbl.id AS [Parent/@Id]
,(SELECT id AS [@id]
,Caption AS [@Caption]
FROM @tblChildren
WHERE ParentId=tbl.id
FOR XML PATH('Child'),TYPE
) AS [Parent/Children]
FROM @tbl AS tbl
FOR XML PATH(''),ROOT('root');