{"id":168469,"date":"2024-11-06T12:38:29","date_gmt":"2024-11-06T04:38:29","guid":{"rendered":"https:\/\/server.hk\/cnblog\/168469\/"},"modified":"2024-11-06T12:38:29","modified_gmt":"2024-11-06T04:38:29","slug":"sql-%e8%aa%9e%e5%8f%a5%e4%b8%ad-output-%e7%9a%84%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/168469\/","title":{"rendered":"SQL \u8a9e\u53e5\u4e2d output \u7684\u7528\u6cd5"},"content":{"rendered":"<h1 id=\"sql-%e8%aa%9e%e5%8f%a5%e4%b8%ad-output-%e7%9a%84%e7%94%a8%e6%b3%95-CwGsLgSIas\">SQL \u8a9e\u53e5\u4e2d OUTPUT \u7684\u7528\u6cd5<\/h1>\n<p>\u5728 SQL \u4e2d\uff0cOUTPUT \u5b50\u53e5\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\uff0c\u5141\u8a31\u7528\u6236\u5728\u57f7\u884c INSERT\u3001UPDATE \u6216 DELETE \u64cd\u4f5c\u6642\uff0c\u6355\u7372\u53d7\u5f71\u97ff\u7684\u884c\u7684\u6578\u64da\u3002\u9019\u5c0d\u65bc\u9700\u8981\u5728\u6578\u64da\u5eab\u64cd\u4f5c\u5f8c\u9032\u884c\u5be9\u8a08\u3001\u8a18\u9304\u6216\u9032\u4e00\u6b65\u8655\u7406\u7684\u60c5\u6cc1\u7279\u5225\u6709\u7528\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e OUTPUT \u5b50\u53e5\u7684\u7528\u6cd5\u53ca\u5176\u5be6\u969b\u61c9\u7528\u3002<\/p>\n<h2 id=\"output-%e5%ad%90%e5%8f%a5%e7%9a%84%e5%9f%ba%e6%9c%ac%e8%aa%9e%e6%b3%95-CwGsLgSIas\">OUTPUT \u5b50\u53e5\u7684\u57fa\u672c\u8a9e\u6cd5<\/h2>\n<p>OUTPUT \u5b50\u53e5\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>\n<em>INSERT\/UPDATE\/DELETE<\/em> <table>\nOUTPUT \n<em>WHERE<\/em> ;\n<\/code><\/pre>\n<p>\u5728\u9019\u88e1\uff0c<code>table_name<\/code> \u662f\u8981\u64cd\u4f5c\u7684\u8868\u540d\uff0c<code>column_list<\/code> \u662f\u8981\u8fd4\u56de\u7684\u5217\uff0c<code>condition<\/code> \u662f\u53ef\u9078\u7684\u689d\u4ef6\uff0c\u7528\u65bc\u9650\u5236\u64cd\u4f5c\u7684\u884c\u3002<\/p>\n<h2 id=\"output-%e5%ad%90%e5%8f%a5%e7%9a%84%e4%bd%bf%e7%94%a8%e7%af%84%e4%be%8b-CwGsLgSIas\">OUTPUT \u5b50\u53e5\u7684\u4f7f\u7528\u7bc4\u4f8b<\/h2>\n<h3 id=\"1-insert-%e6%93%8d%e4%bd%9c%e4%b8%ad%e7%9a%84-output-CwGsLgSIas\">1. INSERT \u64cd\u4f5c\u4e2d\u7684 OUTPUT<\/h3>\n<p>\u7576\u6211\u5011\u57f7\u884c INSERT \u64cd\u4f5c\u6642\uff0c\u53ef\u4ee5\u4f7f\u7528 OUTPUT \u5b50\u53e5\u4f86\u8fd4\u56de\u65b0\u63d2\u5165\u7684\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>\nINSERT INTO Employees (Name, Position)\nOUTPUT INSERTED.EmployeeID, INSERTED.Name\nVALUES ('John Doe', 'Developer');\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u7576\u6211\u5011\u63d2\u5165\u4e00\u540d\u65b0\u54e1\u5de5\u6642\uff0cOUTPUT \u5b50\u53e5\u6703\u8fd4\u56de\u65b0\u54e1\u5de5\u7684 ID \u548c\u59d3\u540d\u3002<\/p>\n<h3 id=\"2-update-%e6%93%8d%e4%bd%9c%e4%b8%ad%e7%9a%84-output-CwGsLgSIas\">2. UPDATE \u64cd\u4f5c\u4e2d\u7684 OUTPUT<\/h3>\n<p>OUTPUT \u5b50\u53e5\u540c\u6a23\u53ef\u4ee5\u7528\u65bc UPDATE \u64cd\u4f5c\uff0c\u4ee5\u4fbf\u5728\u66f4\u65b0\u884c\u6642\u8fd4\u56de\u820a\u503c\u548c\u65b0\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>\nUPDATE Employees\nSET Position = 'Senior Developer'\nOUTPUT DELETED.Position AS OldPosition, INSERTED.Position AS NewPosition\nWHERE Name = 'John Doe';\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u6703\u66f4\u65b0\u540d\u70ba John Doe \u7684\u54e1\u5de5\u7684\u8077\u4f4d\uff0c\u4e26\u8fd4\u56de\u66f4\u65b0\u524d\u5f8c\u7684\u8077\u4f4d\u3002<\/p>\n<h3 id=\"3-delete-%e6%93%8d%e4%bd%9c%e4%b8%ad%e7%9a%84-output-CwGsLgSIas\">3. DELETE \u64cd\u4f5c\u4e2d\u7684 OUTPUT<\/h3>\n<p>\u5728 DELETE \u64cd\u4f5c\u4e2d\uff0cOUTPUT \u5b50\u53e5\u53ef\u4ee5\u7528\u4f86\u8fd4\u56de\u88ab\u522a\u9664\u7684\u884c\u7684\u6578\u64da\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>\nDELETE FROM Employees\nOUTPUT DELETED.EmployeeID, DELETED.Name\nWHERE Name = 'John Doe';\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u6703\u522a\u9664\u540d\u70ba John Doe \u7684\u54e1\u5de5\uff0c\u4e26\u8fd4\u56de\u88ab\u522a\u9664\u54e1\u5de5\u7684 ID \u548c\u59d3\u540d\u3002<\/p>\n<h2 id=\"output-%e5%ad%90%e5%8f%a5%e7%9a%84%e6%87%89%e7%94%a8%e5%a0%b4%e6%99%af-CwGsLgSIas\">OUTPUT \u5b50\u53e5\u7684\u61c9\u7528\u5834\u666f<\/h2>\n<p>OUTPUT \u5b50\u53e5\u7684\u61c9\u7528\u5834\u666f\u975e\u5e38\u5ee3\u6cdb\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u4f7f\u7528\u60c5\u6cc1\uff1a<\/p>\n<ul>\n<li><strong>\u5be9\u8a08\u548c\u65e5\u8a8c\u8a18\u9304\uff1a<\/strong>\u5728\u6578\u64da\u5eab\u64cd\u4f5c\u5f8c\uff0c\u8a18\u9304\u8b8a\u66f4\u7684\u8a73\u7d30\u4fe1\u606f\uff0c\u4ee5\u4fbf\u9032\u884c\u5be9\u8a08\u3002<\/li>\n<li><strong>\u6578\u64da\u540c\u6b65\uff1a<\/strong>\u5728\u591a\u500b\u6578\u64da\u5eab\u4e4b\u9593\u540c\u6b65\u6578\u64da\u6642\uff0c\u4f7f\u7528 OUTPUT \u5b50\u53e5\u4f86\u6355\u7372\u8b8a\u66f4\u7684\u6578\u64da\u3002<\/li>\n<li><strong>\u89f8\u767c\u5668\uff1a<\/strong>\u5728\u89f8\u767c\u5668\u4e2d\u4f7f\u7528 OUTPUT \u5b50\u53e5\u4f86\u7372\u53d6\u88ab\u5f71\u97ff\u7684\u884c\uff0c\u9032\u884c\u9032\u4e00\u6b65\u7684\u8655\u7406\u3002<\/li>\n<\/ul>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-CwGsLgSIas\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u4f7f\u7528 OUTPUT \u5b50\u53e5\u6642\uff0c\u6709\u5e7e\u9ede\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li>OUTPUT \u5b50\u53e5\u7684\u6027\u80fd\u53ef\u80fd\u6703\u53d7\u5230\u5f71\u97ff\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\u3002<\/li>\n<li>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0cOUTPUT \u5b50\u53e5\u53ef\u80fd\u6703\u5c0e\u81f4\u6b7b\u9396\uff0c\u56e0\u6b64\u5728\u8a2d\u8a08\u6578\u64da\u5eab\u64cd\u4f5c\u6642\u9700\u8981\u8b39\u614e\u8003\u616e\u3002<\/li>\n<li>OUTPUT \u5b50\u53e5\u7684\u4f7f\u7528\u53ef\u80fd\u6703\u53d7\u5230\u6578\u64da\u5eab\u7248\u672c\u7684\u9650\u5236\uff0c\u78ba\u4fdd\u6240\u4f7f\u7528\u7684 SQL Server \u7248\u672c\u652f\u6301\u6b64\u529f\u80fd\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-CwGsLgSIas\">\u7e3d\u7d50<\/h2>\n<p>OUTPUT \u5b50\u53e5\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5728\u57f7\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\u6355\u7372\u548c\u8655\u7406\u6578\u64da\u3002\u7121\u8ad6\u662f\u5728\u5be9\u8a08\u3001\u6578\u64da\u540c\u6b65\u9084\u662f\u89f8\u767c\u5668\u4e2d\uff0cOUTPUT \u5b50\u53e5\u90fd\u80fd\u63d0\u4f9b\u91cd\u8981\u7684\u652f\u6301\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u7ba1\u7406\u6578\u64da\u7684\u4f01\u696d\u4f86\u8aaa\uff0c\u4e86\u89e3\u548c\u638c\u63e1 OUTPUT \u5b50\u53e5\u7684\u7528\u6cd5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f\u6578\u64da\u5eab\u7ba1\u7406\u9084\u662f\u7db2\u7ad9\u6258\u7ba1\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3 SQL \u8a9e\u53e5\u4e2d OUTPUT \u7684\u7528\u6cd5\uff0c\u5b78\u7fd2\u5982\u4f55\u5728\u6578\u64da\u64cd\u4f5c\u4e2d\u6355\u7372\u548c\u8fd4\u56de\u8b8a\u66f4\u7684\u6578\u64da\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-168469","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168469","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=168469"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168469\/revisions"}],"predecessor-version":[{"id":168470,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168469\/revisions\/168470"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=168469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=168469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=168469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}