在SQL SERVER 2005 中使用XML

对于XML字段,推荐使用:
<Root>
<ProductDescription ProductID="1" ProductName="Road Bike">
<Features>
  <Warranty>1 year parts and labor</Warranty>
  <Maintenance>3 year parts and labor extended maintenance is available</Maintenance>
</Features>
</ProductDescription>
</Root>

不推荐使用相同的元素
<Root>
<add key="ProductDescription" value="1" />
<add key="ProductDescription" value="2" />
</Root>

modify (XML_DML)
此方法使用 XML DML 语句在 xml 数据中插入、更新或删除节点。xml 数据类型的 modify() 方法只能在 UPDATE 语句的 SET 子句中使用。


insert
      Expression1 (
                 {
as  first  |   as  last} into  |  after  |  before
                                    Expression2
                )

CREATE TABLE TEST
(
   A INT,
   B XML
)

insert test (a,b)
select 
1 , ' <root/> '

-- 插入元素
update test 
set  b.modify( '
insert ( < name > b2 </ name > )
as  first
into (
/ root)[ 1 ] '
)
where a 
=   1

-- 插入属性
update test 
set  b.modify( '
insert (attribute id{ " 001 " })
into (
/ root)[ 1 ] '
)
where a 
=   1


-- 插入注释
update test 
set  b.modify( '
insert  <!--  comment  -->
into (
/ root / name[ 1 ])[ 1 ] '
)
where a 
=   1

-- 使用IF
update test 
set  b.modify( '
insert 
if  (count( / root / name) <= 2 )
then element address {
" China " }
else  ()
as  last
into (
/ root)[ 1 ] '
)
where a 
=   1

-- 替换
update  test 
set  b.modify( '
replace value of (/root/address/text())[1] 
with (
if (count(/root/name)<=2)
then
    "beijin"
else
    "taiwan"
)
'
)
where  a  =   1

-- 删除
update  test 
set  b.modify( '
delete /root/address
' )
where  a  =   1


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值