`

db2 xml查询

 
阅读更多

系统有些表类型是xml结构,db2在9.5以后提供了xml数据字段类型,基于xmlquery可以很做xml字段的更新的,添加属性等操作,

 

update biz.rei_form_detail

set defined_field = xmlquery(' 

              copy $new := $c

              modify do replace value of $new/root/Field[@FieldName="InTime"]/@FieldName with "ReiContent"

              return  $new' passing defined_field as "c" ) 

where defined_field is not null ;

这个是用defined_field字段是一个xml,通过上面的语句就可以

<root><Field FieldName="InTime"></Field ></root>更新成<root><Field FieldName="ReiContent"></Field ></root>

同时xml查询有一个非常有用的聚集行数xmlagg,可以把xml的查询数据分组例如

select user_id, XMLSERIALIZE(xmlagg(xmlelement(NAME A,role_id)) as varchar(5000))  from  biz.saas_user_role group by user_id

假设数据为

user_id  role_id

1           2

1          3

2          1

查询的出来的结果就是

 

1   <A>2</A><A>3</A>

2   <A>1</A>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics