{"id":1467,"date":"2010-12-03T02:58:19","date_gmt":"2010-12-03T02:58:19","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=1467"},"modified":"2011-08-10T03:15:21","modified_gmt":"2011-08-10T03:15:21","slug":"%e5%a4%9a%e8%a1%8c%e5%90%88%e4%b8%80-xml-path","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2010\/12\/03\/%e5%a4%9a%e8%a1%8c%e5%90%88%e4%b8%80-xml-path\/","title":{"rendered":"\u591a\u884c\u5408\u4e00-XML PATH"},"content":{"rendered":"<pre lang=\"tsql\">\r\nDECLARE @TempTable table(UserID int , UserName nvarchar(50));\r\ninsert into @TempTable (UserID,UserName) values (1,'a')\r\ninsert into @TempTable (UserID,UserName) values (1,'b')\r\ninsert into @TempTable (UserID,UserName) values (1,'c')\r\ninsert into @TempTable (UserID,UserName) values (2,'d')\r\ninsert into @TempTable (UserID,UserName) values (2,'e')\r\ninsert into @TempTable (UserID,UserName) values (2,'f')\r\n\r\n--\u4ee5XML\u8282\u70b9\u5f0f\u4e00\u884c\u5b58\u6570\u636e\uff0c\u8282\u70b9\u540d\u5373\u662f\u5217\u540d\r\nselect UserID,UserName from @TempTable FOR XML PATH\r\n\r\n--PATH\u4e2d\u7684\u5185\u5bb9\u6307\u5b9a\u6839\u7ed3\u70b9\u540d\u79f0\uff0c\u82e5\u65e0\uff0c\u5219\u9ed8\u8ba4\u4e3aroot\r\nselect UserID,UserName from @TempTable FOR XML PATH('lzy')\r\n\r\n--\u6307\u5b9a\u9876\u7ea7\u6839\u7ed3\u70b9\u540d\u79f0\r\nselect UserID,UserName from @TempTable FOR XML PATH('lzy'), root ('Root')\r\n\r\n--\u82e5\u7ed3\u679c\u96c6\u4e2d\u65e0\u5217\u540d\uff0c\u90a3\u4e48\u8282\u70b9\u540d\u4e5f\u5c31\u6ca1\u6709\uff0c\u6570\u636e\u5c31\u663e\u793a\u5230\u4e86\u4e00\u884c\r\nselect convert(varchar(50),UserName) +','? from @TempTable s WHERE s.UserID = 1\r\n\r\n--stuff\u7528\u6765\u53bb\u6389\u591a\u4f59\u7684\u9017\u53f7\r\nSELECT DISTINCT t.UserID,\r\nSTUFF((select ',' + convert(varchar(50),UserName)? from @TempTable s WHERE s.UserID = t.UserID FOR XML PATH('')),1,1,'')\r\nFROM @TempTable t\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>DECLARE @TempTable table(UserID int , UserName nvarchar [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-1467","post","type-post","status-publish","format-standard","hentry","category-sql-server"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/1467","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/comments?post=1467"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/1467\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=1467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=1467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=1467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}