sql 和xml
一·常用基本功能
表結果:
1.
1 <row>2 <ID>1035</ID>3 <Rate>6.12</Rate>4 <Date>2013-11-28</Date>5 <Remark>正常</Remark>6 </row>7 <row>8 <ID>1036</ID>9 <Rate>6.15</Rate> 10 <Date>2013-11-28</Date> 11 <Remark>正常</Remark> 12 </row> 1 SELECT * 2 FROM dbo.T_H_ExchangeRate 3 FOR XML PATH2.
1 <ID>1035</ID> 2 <Rate>6.12</Rate> 3 <Date>2013-11-28</Date> 4 <Remark>正常</Remark> 5 <ID>1036</ID> 6 <Rate>6.15</Rate> 7 <Date>2013-11-28</Date> 8 <Remark>正常</Remark> 1 SELECT * 2 FROM dbo.T_H_ExchangeRate 3 FOR XML PATH('')3.
<SUN><ID>1035</ID><Rate>6.12</Rate><Date>2013-11-28</Date><Remark>正常</Remark> </SUN> <SUN><ID>1036</ID><Rate>6.15</Rate><Date>2013-11-28</Date><Remark>正常</Remark> </SUN> SELECT * FROM dbo.T_H_ExchangeRate FOR XML PATH('SUN')4.
<漢語><序號>1035</序號><匯率>6.12</匯率><日期>2013-11-28</日期><標記>正常</標記> </漢語> <漢語><序號>1036</序號><匯率>6.15</匯率><日期>2013-11-28</日期><標記>正常</標記> </漢語> SELECT ID AS '序號',Rate AS '匯率',Date AS '日期',Remark AS '標記' FROM dbo.T_H_ExchangeRate FOR XML PATH('漢語')5.
--生成帶頂層節點<Table><Row><ID>1035</ID><Rate>6.12</Rate><Date>2013-11-28</Date><Remark>正常</Remark></Row><Row><ID>1036</ID><Rate>6.15</Rate><Date>2013-11-28</Date><Remark>正常</Remark></Row> </Table> SELECT * FROM dbo.T_H_ExchangeRate FOR XML PATH('Row'),ROOT('Table') --還原成DataTable的形式,查找節點 SELECT T.x.value('(ID)[1]','nvarchar(50)') AS ID,T.x.value('(Rate)[1]','nvarchar(50)') AS Rate,T.x.value('(Date)[1]','nvarchar(50)') AS Date,T.x.value('(Remark)[1]','nvarchar(50)') AS Remark FROM @xmlStr.nodes('/Table/Row') T(x)
?
?
6.
[正常],[正常], SELECT '['+Remark+'],' FROM dbo.T_H_ExchangeRate FOR XML PATH('')7.
數據源:
?
查詢結果:
--源代碼SELECT C.O_USER, LEFT(C.Operate,LEN(Operate)-1) Operate FROM ( SELECT A.O_USER,(SELECT B.O_TYPE+','FROM dbo.T_UP_Olog AS BWHERE B.O_USER = A.O_USERFOR XML PATH('')) Operate FROM dbo.T_UP_Olog A GROUP BY A.O_USER )AS C
?
轉載于:https://www.cnblogs.com/yujihaia/p/7397949.html
總結
- 上一篇: C#基础知识 课堂笔记 上
- 下一篇: 【模式匹配】KMP算法的来龙去脉