SQL“For XML Path” – 嵌套结果
发布时间:2020-12-12 16:53:29 所属栏目:MsSql教程 来源:网络整理
导读:我有这个表结构. YearPart,MonthPart和DatePart包含他们描述的内容… EX:2011,1,19(分别) DECLARE @agenda AS TABLE ( PID INT IDENTITY(1,1) PRIMARY KEY,YearPart int,MonthPart int,DayPart int,lib_title nvarchar(200),[filename] nvarchar(255),meta_v
我有这个表结构. YearPart,MonthPart和DatePart包含他们描述的内容… EX:2011,1,19(分别)
DECLARE @agenda AS TABLE ( PID INT IDENTITY(1,1) PRIMARY KEY,YearPart int,MonthPart int,DayPart int,lib_title nvarchar(200),[filename] nvarchar(255),meta_value nvarchar(2000) ) 使用此示例数据: INSERT INTO @agenda VALUES (2010,12,4,'Test Record','','') INSERT INTO @agenda VALUES (2011,3,'Another Record','Fred Birthday','Work Day',6,'2nd Test Record','') 我想要的是一个这样的XML输出: <root> <Year Year="2010"> <Month Month="12"> <Day Day="4"> <Item RecordName="Test Record" RecordID="1" /> </Day> </Month> </Year> <Year Year="2011"> <Month Month="1"> <Day Day="3"> <Item RecordName="Another Record" RecordID="2" /> <Item RecordName="Geoffrey Birthday" RecordID="3" /> </Day> <Day Day="4"> <Item RecordName="Work Day" RecordID="4" /> </Day> </Month> <Month Month="12"> <Day Day="6"> <Item RecordName="2nd Test Record" RecordID="5" /> </Day> </Month> </Year> </root> 到目前为止,我还没有得到嵌套正常工作.我通常结束分组(例如,我得到多个Year = 2011元素,当只应该有一个). 如果这不能完成,我可以随时在.NET网站上创建XML 解决方法可以做到select a1.YearPart as '@Year',( select MonthPart as '@Month',(select DayPart as '@Day',(select lib_title as '@RecordName',PID as '@RecordID' from @agenda as a4 where a4.DayPart = a3.DayPart and a4.MonthPart = a2.MonthPart and a4.YearPart = a1.YearPart for xml path('Item'),type ) from @agenda as a3 where a3.YearPart = a1.YearPart and a3.MonthPart = a2.MonthPart group by a3.DayPart for xml path('Day'),type ) from @agenda as a2 where a1.YearPart = a2.YearPart group by a2.MonthPart for xml path('Month'),type ) from @agenda as a1 group by YearPart for xml path('Year'),root (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- sqlserver列出当前数据库中的表的行数列数
- 实战:sqlserver 2008 R2容灾方案:另一种思路-2
- SQLSERVER的查询结果复制到EXCEL带标题行
- SQLSERVER 2005的ROW_NUMBER、RANK、DENSE_RANK的用法
- sql-server – 返回id并拆分逗号分隔值的SQL语句
- SQLServer中使用javascript脚本验证数据
- SQL ORACLE ORA-00969:缺少ON关键字?
- MySQL ERROR 1045 (28000): Access denied for user 'r
- SQLServer 2008数据库备份和还原
- SQL建立数据库及删除数据库命令
推荐文章
站长推荐
- SQL SERVER 2008 r2 数据压缩的两种方法
- sql-server-2008 – SQL Server中“无连接谓词”
- 在SQL Server数据库中的非IDENTITY类型字段中重新
- sqlserver 分页
- sql-server – INSERT OVER语句?
- SQLSERVER2005 MANAGEMENT STIDO EXPRESS 下载地
- sqlserver在调用存储过程中遇到convert转换失败问
- SQLServer2012 和 MariaDB 10.0.3 分页效率的对比
- Jim Gray Summary Home Page
- 在SQL Server Management Studio中快速从C#运行时
热点阅读