sqlserver2005 xml字段的读写操作
发布时间:2024-01-16 23:00:37
1.创建xml字段
在表xmlTest内创建一个字段,命名_x,设置数据类型为xml。
2.写入xml
insert into xmlTest (_x) values('
<root>
<item attr1="a1" attr2="a2">item11</item>
<item attr1="a1" attr2="a2">item12</item>
</root>
')
go
如此再插入一条记录:
insert into xmlTest (_x) values('
<root>
<item attr1="b1" attr2="b2">item21</item>
<item attr1="b1" attr2="b2">item22</item>
</root>
')
3.查询数据
3.1 查询字段所有数据
select _x from xmlText
结果如图:
3.2 查询root下面的第一个item节点
select _x.query('root/item[1]')
from xmlTest
结果:
3.3 查询root下面的第一个item节点的值
select _x.value('(root/item)[1]','varchar(50)')
from xmlTest
结果:
3.3 查询root下面的第二个item节点的attr2属性值
select _x.value('(root/item/@attr2)[2]','varchar(50)')
from xmlTest
结果:
3.4 查询表中每条记录的xml字段的item节点值,全部列出来
SELECT t2.c1.value('.','varchar(20)') as items
from xmlTest
cross apply _x.nodes('root/item') as t2(c1)
结果:
3.5 查询表中id=1记录的xml的所有item字段的attr2属性值
SELECT t2.c1.value('@attr2','varchar(20)') as attr2
from xmlTest
cross apply _x.nodes('root/item') as t2(c1)
where id='1'
结果:
3.6 查询id为1的所有节点中attr1属性为a1的第一个匹配节点的attr2属性值
select _x.value('(root/item[@attr1="a1"]/@attr2)[1]','varchar(50)')
from xmlTest where id='1'
结果:
3.7 多表查询
例如:存在另外1个table:t2,其字段xmlid关联表xmlTest,而attr1关联xmlTest的_x字段中的attr1属性值。
查询表xmlTest中id=‘1'记录中_x字段内item节点中attr1为表t2中attr1字段值时,item节点中attr2的属性值
select xmlTest._x.value('(root/item[@attr1=sql:column("t2.attr1")]/@attr2)[1]','varchar(50)') as attr2,t2.attr1
from xmlTest join t2 on t2.xmlid=xmlTest.id
where xmlTest.id='1'
4.修改数据
4.1 修改表中id=1记录的第一个item节点值
UPDATE xmlTest
set _x.modify('replace value of (root/item/text())[1] with "xxxxx"')
where id='1'
select _x.query('root/item[1]') as item1
from xmlTest
结果:
4.2 修改表中id=2记录的第二个item节点的attr2属性值
UPDATE xmlTest
set _x.modify('replace value of (root/item/@attr2)[2] with "2222b"')
where id='2'
select _x.query('root/item[2]') as item1
from xmlTest
结果:
猜你喜欢
- 喜欢用Python写脚本的小伙伴可以跟着一起写一写呀。编写环境:Python2.x00x1:需要用到的模块需要用到的模块如下:import
- 情况一本地导出,远程导入(windows导到linux)本机(物理机)数据库全部导出(左下角打开dos窗口)1.启动mysqlnet sta
- 一、迭代器(foreach)1、可迭代的对象内置有__iter__方法的都叫可迭代的对象。Python内置str、list、tuple、di
- 哲学上有种说法,“运动是绝对的,静止是相对的”。我们在编写各样的效果时,时常会碰到动画。下面的章,将讨论动画的原理以及实现。动画,简而言之就
- 之前遇到过一类问题,要求快速做文件搜索,当时小编找了很多内容,但是没有发现实现方法,突然看到glob模块便豁然开朗了,该模块主要就是能够实现
- Python 多线程的实例详解一)线程基础1、创建线程:thread模块提供了start_new_thread函数,用以创建线程
- 最近在做新闻发布系统的时候,用到了二级联动,我把使用方法记录下来,以便日后查阅以及帮助新手朋友们。下面是效果图:下面来讲解一下实现的方法:1
- 利用python pyheatmap包绘制热力图,供大家参考,具体内容如下import matplotlib.pyplot as pltfr
- 如何实现自定义一个异常python内置了许多异常类,为编写代码划定红线,才使调试代码时能及时发现错误。那么我们编写一个模块也可以为使用此模块
- python具体强大的库文件,很多功能都有相应的库文件,所以很有必要进行学习一下,其中有一个ftp相应的库文件ftplib,我们只需要其中的
- 本文实例讲述了Python导出DBF文件到Excel的方法。分享给大家供大家参考。具体如下:from dbfpy import dbffro
- 安装PIL在Debian/Ubuntu Linux下直接通过apt安装:$ sudo apt-get install python-imag
- 服务器响应HTTP的类型ContentType大全,使用方法:<% Response.ContentType =&
- 使用ASP实现网站的目录树数据库结构(共使用了两个表)1。tblCategory字段名 类型 Root&
- 声明定位元素:position属性值设置除默认值static以外的元素,包括relative,absolute,fixed。平台:win/I
- 1 conda介绍conda是一个python的包管理器,用来管理、安装、更新python的包和相关的依赖。另外,conda可以为特定任务创
- 思路非常简单:<p>创建window,设置窗口大小,创建label1,导入图片,创建label2,导入文字,show,结束!&l
- 我就废话不多说了,直接上代码吧!import numpy as npimport torchimport torch.nn as nnimp
- 首先,我的索引结构是酱紫的。
- 一、模拟登录图书馆管理系统我们可以先看一下登录页面(很多学校这些管理系统页面就是很low):两种方式去模拟登录图书馆:1. 构造登录表单进行