SELECTT.C.value('.','varchar(125)') AS Barcode--,T.C.value('WarehouseID[1]','bigint') AS WarehouseID
--, ItemCode = @DefaultString125--, ItemName = @DefaultString125--, WarehouseCode = @DefaultString125--, WarehouseName = @DefaultString125
into #hbh_tmp_BarcodeList
FROM @Barcode.nodes('//string') AS T(C)
-- http://bbs.csdn.net/topics/340010416
/*sql xml 入门: --by jinjazz --http://blog.csdn.net/jinjazz
1、xml: 能认识元素、属性和值
2、xpath: 寻址语言,类似windows目录的查找(没用过dir命令的话就去面壁)
语法格式,这些语法可以组合为条件: "."表示自己,".."表示父亲,"/"表示儿子,"//"表示后代, "name"表示按名字查找,"@name"表示按属性查找
"集合[条件]" 表示根据条件取集合的子集,条件可以是 数 值:数字,last(),last()-数字 等 布尔值:position()<数字,@name='条件',name='条件' 条件是布尔值的时候可以合并计算:and or
3、xquery: 基于xpath标的准查询语言,sqlserver xquery包含如下函数 exist(xpath条件):返回布尔值表示节点是否存在 query(xpath条件):返回由符合条件的节点组成的新的xml文档 value(xpath条件,数据类型):返回指定的标量值,xpath条件结果必须唯一 nodes(xpath条件): 返回由符合条件的节点组成的一行一列的结果表*/
declare @data xmlset @data=' Giada De Laurentiis 2005
30.00 J K. Rowling 2005
29.99 James McGovern Per Bothner Kurt Cagle James Linn Vaidyanathan Nagarajan 2003
49.99 Erik T. Ray 2003
39.95'
--测试语句,如果不理解语法请参考上面的xpath规则和xquery函数说明
--1、文档select @data--2、任意级别是否存在price节点select @data.exist('//price')--3、获取所有book节点select @data.query('//book')--4、获取所有包含lang属性的节点select @data.query('//*[@lang]') --5、获取第一个book节点select @data.query('//book[1]')--6、获取前两个book节点select @data.query('//book[position()<=2]')--7、获取最后一个book节点select @data.query('//book[last()]')--8、获取price>35的所有book节点select @data.query('//book[price>35]')--9、获取category="WEB"的所有book节点select @data.query('//book[@category="WEB"]')--10、获取title的lang="en"的所有book节点select @data.query('//book/title[@lang="en"]')--11、获取title的lang="en"且 price>35的所有book节点select @data.query('//book[./title[@lang="en"] or price>35 ]')--12、获取title的lang="en"且 price>35的第一book的(第一个)titleselect @data.query('//book[./title[@lang="en"] and price>35 ]').value('(book/title)[1]','varchar(max)')--13、等价于12select @data.value('(//book[./title[@lang="en"] and price>35 ]/title)[1]','varchar(max)')--14、获取title的lang="en"且 price>35的第一book的(第一个)title的lang属性select @data.value('((//book[@category="WEB" and price>35 ]/title)[1]/@lang)[1]','varchar(max)')--15、获取第一本书的titleselect Tab.Col.value('(book/title)[1]','varchar(max)') as title from @data.nodes('bookstore')as Tab(Col) --16、获取每本书的第一个authorselect Tab.Col.value('author[1]','varchar(max)') as title from @data.nodes('//book')as Tab(Col)--17、获取所有book的所有信息select T.C.value('title[1]','varchar(max)') as title, T.C.value('year[1]','int') as year, T.C.value('title[1]','varchar(max)')as title, T.C.value('price[1]','float') as price, T.C.value('author[1]','varchar(max)') as author1, T.C.value('author[2]','varchar(max)') as author2, T.C.value('author[3]','varchar(max)') as author3, T.C.value('author[4]','varchar(max)') as author4from @data.nodes('//book') as T(C)--18、获取不是日语(lang!="jp")且价格大于35的书的所有信息select T.C.value('title[1]','varchar(max)') as title, T.C.value('year[1]','int') as year, T.C.value('title[1]','varchar(max)')as title, T.C.value('price[1]','float') as price, T.C.value('author[1]','varchar(max)') as author1, T.C.value('author[2]','varchar(max)') as author2, T.C.value('author[3]','varchar(max)') as author3, T.C.value('author[4]','varchar(max)') as author4from @data.nodes('//book[./title[@lang!="jp"] and price>35 ]') as T(C)
-- http://bbs.csdn.net/topics/391050526-- 用xml和cross jon 做字符串分拆成表行
DECLARE @STRIN VARCHAR(MAX)SET @STRIN='87349+2015-06-10 10:38_2015-06-23 10:38+20.00 $109603+2015-06-16 10:38_2015-06-22 10:38+0.00';WITH CTE AS(SELECT RTRIM(T2.V)V,CHARINDEX('+',T2.V)N1,CHARINDEX('_',T2.V)N2,CHARINDEX('+',T2.V,CHARINDEX('+',T2.V)+1)N3FROM(SELECT CAST(''+REPLACE(@STRIN,'$','')+''AS XML)VS)T1CROSS APPLY(SELECT N.V.value('.','VARCHAR(100)')V FROM T1.VS.nodes('/V')N(V))T2),CTE2 AS(SELECT LEFT(V,N1-1)AS[stuID],SUBSTRING(V,N1+1,N2-N1-1)[date1],SUBSTRING(V,N2+1,N3-N2-1)[date2],RIGHT(V,LEN(V)-N3)[FeeStand]FROM CTE)SELECT [stuID],[date],[FeeStand]FROM CTE2 UNPIVOT([date] FOR T IN([date1],[date2]))U
-- wf
-- 1、获得传入物料ID集合If OBJECT_ID('tempdb..#hbh_tmp_ItemList') is not nullDrop Table #hbh_tmp_ItemList
declare @split char(1) = ','DECLARE @xmlIDs XML
SET @xmlIDs = CONVERT(XML,'')
-- select *-- into #hbh_tmp_ItemList-- from CBO_ItemMaster-- where ID in (-- SELECT x.item.value('@id[1]', 'bigint') as ItemID-- FROM @xmlIDs.nodes('//items/item') AS x(item)-- )
SELECT x.item.value('@id[1]', 'bigint') as ItemIDinto #hbh_tmp_ItemListFROM @xmlIDs.nodes('//items/item') AS x(item)
-- 拼接字符串select stuff((select ',' + convert(varchar(125),cast(ID as varchar(125)) )from Cust_SalesmanCreditfor xml path('')),1,1,'')
-- 表转xml
select -- rtrim(sno) as "@no",rtrim(sname) as "@name",rtrim(ssex) as "@sex"
ID, Code, CreatedOn, CreatedBy, ModifiedOn, ModifiedBy, IsBase, UOMClass, Round_Precisionfrom Base_Uomfor xml path('Uom'),type,root('AllUOMs')
declare @MOXml xml = null
set @MOXml = (select -- rtrim(sno) as "@no",rtrim(sname) as "@name",rtrim(ssex) as "@sex"
ID, Code, CreatedOn, CreatedBy, ModifiedOn, ModifiedBy, IsBase, UOMClass, Round_Precisionfrom Base_Uomfor xml path('Uom'),type,root('AllUOMs')
)
select @MOXml
【文章原创作者:阿里云代理 http://www.558idc.com/aliyun.html 复制请保留原URL】