如何重命名sql.xml查询xml元素的形状

时间:2010-12-18 12:15:17

标签: sql tsql sql-server-2008

我有以下查询

insert @p(target,type,val,[order])
        select "/session/verbs/put[@field='orno.f']",'string',141527,1
union   select "/session/verbs/put[@field='orno.t']","string",141527,2
union   select "/session/verbs/put[@field='orno.t']","string",141527,2
union   select "/session/verbs/put[@field='comp.f']","string",000,2
union   select "/session/verbs/put[@field='comp.t']","string",999,2.1
union   select "/session/verbs/setreport/parameter[@name='device']","string",105,3

set quoted_identifier on
SELECT  (SELECT target, type
         FROM @p

         FOR XML RAW, TYPE,ROOT('session')).query('/*');

然后返回

<session>
  <row target="/session/verbs/put[@field='comp.f']" type="string" />
  <row target="/session/verbs/put[@field='comp.t']" type="string" />
  <row target="/session/verbs/put[@field='orno.f']" type="string" />
  <row target="/session/verbs/put[@field='orno.t']" type="string" />
  <row target="/session/verbs/setreport/parameter[@name='device']" type="string" />

如何修改查询以将<row>重命名为<parameter>

1 个答案:

答案 0 :(得分:2)

如果您使用SQL Server 2008(R2),则可以在RAW derective之后传递参数名称:

FOR XML RAW('parameter'), TYPE,ROOT('session')).query('/*');