如何在打开和关闭标记tsql之间获取xml raw值

时间:2017-03-14 10:36:36

标签: sql-server xml tsql sql-server-2012

我要求输出为 -

<g> 
<text x="480" y="846">Dakshin Bastar Dantewada</text>
<text x="460" y="833">Bijapur</text>
<text x="475" y="809">Narayanpur</text>
<text x="475" y="792">Uttar Bastar Kanker</text>
<text x="466" y="765">Rajnandgaon</text>
</g>

使用for xml raw,我的输出为 -

<g> 
<text x="480" y="846" DistrictName = "Dakshin Bastar Dantewada" />
<text x="460" y="833" DistrictName = "Bijapur" />
<text x="475" y="809" DistrictName = "Narayanpur" />
<text x="475" y="792" DistrictName = "Uttar Bastar Kanker" />
<text x="466" y="765" DistrictName = "Rajnandgaon" />
</g>

我正在尝试的代码是: -

SELECT     
               ( SELECT
                      X AS x ,
                      Y AS y ,
                      DISTRICTNAME
                      FROM
                      dbo.MYDISTRICT d1
                      WHERE
                      d1.STATEID = @pStateId
                 FOR
                    XML RAW('text') ,
                    TYPE
                 )
FROM      dbo.MYSTATE
WHERE     dbo.MYSTATE.STATEID = @pStateId AND           dbo.MYSTATE.COUNTRYID = @pCountryId

FOR
XML RAW('g'), TYPE

我需要在文本xml原始开始和结束标记之间使用区域名称。

感谢任何帮助。

非常感谢。

1 个答案:

答案 0 :(得分:1)

使用RAW,您可以获得属性元素。如果要详细指定结构,请使用FOR XML PATH

SELECT     
               ( 
               SELECT
                      X AS [text/@x],
                      Y AS [text/@y] ,
                      DISTRICTNAME AS [text]
                      FROM
                      dbo.MYDISTRICT d1
                      WHERE
                      d1.STATEID = @pStateId
                 FOR
                    XML PATH(''),
                    TYPE
                 )
FROM      dbo.MYSTATE
WHERE     dbo.MYSTATE.STATEID = @pStateId AND           dbo.MYSTATE.COUNTRYID = @pCountryId

FOR
XML RAW('g')