{"id":139652,"date":"2024-10-31T19:31:24","date_gmt":"2024-10-31T11:31:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/139652\/"},"modified":"2024-10-31T19:31:25","modified_gmt":"2024-10-31T11:31:25","slug":"sql-server%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%adfor-xml-auto%e7%9a%84%e4%bd%bf%e7%94%a8%e8%a9%b3%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/139652\/","title":{"rendered":"SQL Server\u6578\u64da\u5eab\u4e2dFOR XML AUTO\u7684\u4f7f\u7528\u8a73\u89e3"},"content":{"rendered":"<h1 id=\"sql-server%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%adfor-xml-auto%e7%9a%84%e4%bd%bf%e7%94%a8%e8%a9%b3%e8%a7%a3-yMYOwgAeZS\">SQL Server\u6578\u64da\u5eab\u4e2dFOR XML AUTO\u7684\u4f7f\u7528\u8a73\u89e3<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u7684\u683c\u5f0f\u5316\u548c\u8f38\u51fa\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002SQL Server\u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u6cd5\u4f86\u5c07\u67e5\u8a62\u7d50\u679c\u8f49\u63db\u70baXML\u683c\u5f0f\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f\u4f7f\u7528<code>FOR XML AUTO<\/code>\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u6df1\u5165\u63a2\u8a0e<code>FOR XML AUTO<\/code>\u7684\u4f7f\u7528\u65b9\u6cd5\u53ca\u5176\u61c9\u7528\u5834\u666f\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%affor-xml-auto%ef%bc%9f-yMYOwgAeZS\">\u4ec0\u9ebc\u662fFOR XML AUTO\uff1f<\/h2>\n<p><code>FOR XML AUTO<\/code>\u662f\u4e00\u7a2eSQL Server\u7684\u67e5\u8a62\u9078\u9805\uff0c\u5141\u8a31\u7528\u6236\u5c07\u67e5\u8a62\u7d50\u679c\u81ea\u52d5\u8f49\u63db\u70baXML\u683c\u5f0f\u3002\u9019\u7a2e\u65b9\u6cd5\u7684\u7279\u9ede\u662f\u7c21\u55ae\u6613\u7528\uff0c\u80fd\u5920\u5feb\u901f\u751f\u6210\u7d50\u69cb\u5316\u7684XML\u6578\u64da\u3002\u7576\u4f7f\u7528<code>FOR XML AUTO<\/code>\u6642\uff0cSQL Server\u6703\u6839\u64da\u67e5\u8a62\u7684\u7d50\u69cb\u81ea\u52d5\u751f\u6210XML\u5143\u7d20\u548c\u5c6c\u6027\u3002<\/p>\n<h2 id=\"%e5%9f%ba%e6%9c%ac%e8%aa%9e%e6%b3%95-yMYOwgAeZS\">\u57fa\u672c\u8a9e\u6cd5<\/h2>\n<p>\u4f7f\u7528<code>FOR XML AUTO<\/code>\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>SELECT column1, column2\nFROM table_name\nFOR XML AUTO;<\/code><\/pre>\n<p>\u5728\u9019\u88e1\uff0c<code>column1<\/code>\u548c<code>column2<\/code>\u662f\u4f60\u5e0c\u671b\u5f9e<code>table_name<\/code>\u4e2d\u9078\u64c7\u7684\u5217\u3002\u9019\u500b\u67e5\u8a62\u5c07\u8fd4\u56de\u4e00\u500bXML\u683c\u5f0f\u7684\u7d50\u679c\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8%e7%a4%ba%e4%be%8b-yMYOwgAeZS\">\u4f7f\u7528\u793a\u4f8b<\/h2>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba<code>Customers<\/code>\u7684\u8868\uff0c\u7d50\u69cb\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li><code>CustomerID<\/code><\/li>\n<li><code>CustomerName<\/code><\/li>\n<li><code>ContactName<\/code><\/li>\n<li><code>Country<\/code><\/li>\n<\/ul>\n<p>\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\u4f86\u7372\u53d6\u8a72\u8868\u7684XML\u683c\u5f0f\u6578\u64da\uff1a<\/p>\n<pre><code>SELECT CustomerID, CustomerName, ContactName, Country\nFROM Customers\nFOR XML AUTO;<\/code><\/pre>\n<p>\u57f7\u884c\u4e0a\u8ff0\u67e5\u8a62\u5f8c\uff0c\u8fd4\u56de\u7684XML\u7d50\u679c\u53ef\u80fd\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code>&lt;Customers&gt;\n    &lt;Customer CustomerID=\"1\" CustomerName=\"Alfreds Futterkiste\" ContactName=\"Maria Anders\" Country=\"Germany\"\/&gt;\n    &lt;Customer CustomerID=\"2\" CustomerName=\"Ana Trujillo Emparedados y helados\" ContactName=\"Ana Trujillo\" Country=\"Mexico\"\/&gt;\n&lt;\/Customers&gt;<\/code><\/pre>\n<h2 id=\"for-xml-auto%e7%9a%84%e7%89%b9%e6%80%a7-yMYOwgAeZS\">FOR XML AUTO\u7684\u7279\u6027<\/h2>\n<p>\u4f7f\u7528<code>FOR XML AUTO<\/code>\u6642\uff0c\u6709\u5e7e\u500b\u7279\u6027\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li><strong>\u81ea\u52d5\u751f\u6210\u7d50\u69cb\uff1a<\/strong>SQL Server\u6703\u6839\u64da\u67e5\u8a62\u7684\u7d50\u69cb\u81ea\u52d5\u751f\u6210XML\u5143\u7d20\uff0c\u9019\u4f7f\u5f97\u7528\u6236\u7121\u9700\u624b\u52d5\u5b9a\u7fa9XML\u7d50\u69cb\u3002<\/li>\n<li><strong>\u5c64\u6b21\u7d50\u69cb\uff1a<\/strong>\u5982\u679c\u67e5\u8a62\u6d89\u53ca\u591a\u500b\u8868\uff0c<code>FOR XML AUTO<\/code>\u6703\u6839\u64da\u8868\u4e4b\u9593\u7684\u95dc\u4fc2\u751f\u6210\u5d4c\u5957\u7684XML\u7d50\u69cb\u3002<\/li>\n<li><strong>\u6027\u80fd\u8003\u91cf\uff1a<\/strong>\u96d6\u7136<code>FOR XML AUTO<\/code>\u4f7f\u7528\u65b9\u4fbf\uff0c\u4f46\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\uff0c\u6027\u80fd\u53ef\u80fd\u6703\u53d7\u5230\u5f71\u97ff\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u6642\u9700\u8b39\u614e\u8003\u91cf\u3002<\/li>\n<\/ul>\n<h2 id=\"%e9%80%b2%e9%9a%8e%e7%94%a8%e6%b3%95-yMYOwgAeZS\">\u9032\u968e\u7528\u6cd5<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7528\u6cd5\u5916\uff0c<code>FOR XML AUTO<\/code>\u9084\u53ef\u4ee5\u8207\u5176\u4ed6SQL Server\u529f\u80fd\u7d50\u5408\u4f7f\u7528\uff0c\u4f8b\u5982<code>WHERE<\/code>\u5b50\u53e5\u548c<code>ORDER BY<\/code>\u5b50\u53e5\uff0c\u4ee5\u9032\u4e00\u6b65\u7be9\u9078\u548c\u6392\u5e8f\u6578\u64da\uff1a<\/p>\n<pre><code>SELECT CustomerID, CustomerName, ContactName, Country\nFROM Customers\nWHERE Country = 'Germany'\nORDER BY CustomerName\nFOR XML AUTO;<\/code><\/pre>\n<p>\u9019\u6a23\u7684\u67e5\u8a62\u5c07\u50c5\u8fd4\u56de\u4f86\u81ea\u5fb7\u570b\u7684\u5ba2\u6236\uff0c\u4e26\u6309\u5ba2\u6236\u540d\u7a31\u6392\u5e8f\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-yMYOwgAeZS\">\u7e3d\u7d50<\/h2>\n<p>\u5728SQL Server\u4e2d\uff0c<code>FOR XML AUTO<\/code>\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u751f\u6210XML\u683c\u5f0f\u7684\u6578\u64da\u3002\u7121\u8ad6\u662f\u5728\u6578\u64da\u5831\u544a\u3001\u6578\u64da\u4ea4\u63db\u9084\u662fAPI\u958b\u767c\u4e2d\uff0c<code>FOR XML AUTO<\/code>\u90fd\u80fd\u63d0\u4f9b\u4fbf\u5229\u3002\u5c0d\u65bc\u9700\u8981\u5728\u9999\u6e2f\u5c0b\u627e\u9ad8\u6548\u80fd\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\u7684\u7528\u6236\uff0c<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u548c<a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a>\u63d0\u4f9b\u4e86\u7a69\u5b9a\u7684\u74b0\u5883\u4f86\u904b\u884cSQL Server\uff0c\u78ba\u4fdd\u6578\u64da\u7684\u5b89\u5168\u548c\u9ad8\u6548\u8655\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u63a2\u8a0eSQL Server\u6578\u64da\u5eab\u4e2dFOR XML AUTO\u7684\u4f7f\u7528\uff0c\u5b78\u7fd2\u5982\u4f55\u8f15\u9b06\u751f\u6210XML\u683c\u5f0f\u7684\u67e5\u8a62\u7d50\u679c\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-139652","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/139652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=139652"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/139652\/revisions"}],"predecessor-version":[{"id":139653,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/139652\/revisions\/139653"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=139652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=139652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=139652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}