{"id":66323,"date":"2024-10-14T16:29:14","date_gmt":"2024-10-14T08:29:14","guid":{"rendered":"https:\/\/server.hk\/cnblog\/66323\/"},"modified":"2024-10-14T16:29:14","modified_gmt":"2024-10-14T08:29:14","slug":"mssql%e4%b8%ad%e7%9a%84%e8%ae%8a%e9%87%8f%e8%b3%a6%e5%80%bc%e6%8a%80%e5%b7%a7%ef%bc%88mssql%e8%ae%8a%e9%87%8f%e8%b3%a6%e5%80%bc%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/66323\/","title":{"rendered":"MSSQL\u4e2d\u7684\u8b8a\u91cf\u8ce6\u503c\u6280\u5de7\uff08mssql\u8b8a\u91cf\u8ce6\u503c\uff09"},"content":{"rendered":"<h1 id=\"mssql%e4%b8%ad%e7%9a%84%e8%ae%8a%e9%87%8f%e8%b3%a6%e5%80%bc%e6%8a%80%e5%b7%a7%ef%bc%88mssql%e8%ae%8a%e9%87%8f%e8%b3%a6%e5%80%bc%ef%bc%89-qYORFrqDmf\">MSSQL\u4e2d\u7684\u8b8a\u91cf\u8ce6\u503c\u6280\u5de7\uff08mssql\u8b8a\u91cf\u8ce6\u503c\uff09<\/h1>\n<p>\u5728Microsoft SQL Server\uff08MSSQL\uff09\u4e2d\uff0c\u8b8a\u91cf\u662f\u4e00\u7a2e\u7528\u65bc\u5b58\u5132\u6578\u64da\u7684\u81e8\u6642\u5bb9\u5668\u3002\u8b8a\u91cf\u7684\u8ce6\u503c\u6280\u5de7\u5c0d\u65bc\u63d0\u9ad8\u67e5\u8a62\u6548\u7387\u548c\u4ee3\u78bc\u53ef\u8b80\u6027\u81f3\u95dc\u91cd\u8981\u3002\u672c\u6587\u5c07\u63a2\u8a0eMSSQL\u4e2d\u8b8a\u91cf\u7684\u8ce6\u503c\u65b9\u6cd5\u53ca\u5176\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2 id=\"%e8%ae%8a%e9%87%8f%e7%9a%84%e5%ae%9a%e7%be%a9%e8%88%87%e8%81%b2%e6%98%8e-qYORFrqDmf\">\u8b8a\u91cf\u7684\u5b9a\u7fa9\u8207\u8072\u660e<\/h2>\n<p>\u5728\u4f7f\u7528\u8b8a\u91cf\u4e4b\u524d\uff0c\u5fc5\u9808\u5148\u9032\u884c\u8072\u660e\u3002\u8b8a\u91cf\u7684\u8072\u660e\u4f7f\u7528\u95dc\u9375\u5b57 <code>DECLARE<\/code>\uff0c\u4e26\u6307\u5b9a\u8b8a\u91cf\u7684\u6578\u64da\u985e\u578b\u3002\u4ee5\u4e0b\u662f\u8b8a\u91cf\u8072\u660e\u7684\u57fa\u672c\u8a9e\u6cd5\uff1a<\/p>\n<pre><code>DECLARE @VariableName DataType;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u82e5\u8981\u8072\u660e\u4e00\u500b\u6574\u6578\u8b8a\u91cf\uff0c\u53ef\u4ee5\u9019\u6a23\u5beb\uff1a<\/p>\n<pre><code>DECLARE @Count INT;<\/code><\/pre>\n<h2 id=\"%e8%ae%8a%e9%87%8f%e7%9a%84%e8%b3%a6%e5%80%bc%e6%96%b9%e6%b3%95-qYORFrqDmf\">\u8b8a\u91cf\u7684\u8ce6\u503c\u65b9\u6cd5<\/h2>\n<p>MSSQL\u4e2d\u6709\u591a\u7a2e\u65b9\u6cd5\u53ef\u4ee5\u70ba\u8b8a\u91cf\u8ce6\u503c\uff0c\u4ee5\u4e0b\u662f\u5e7e\u7a2e\u5e38\u898b\u7684\u65b9\u6cd5\uff1a<\/p>\n<h3>1. \u4f7f\u7528 <code>SET<\/code> \u8a9e\u53e5<\/h3>\n<p>\u6700\u5e38\u898b\u7684\u8ce6\u503c\u65b9\u6cd5\u662f\u4f7f\u7528 <code>SET<\/code> \u8a9e\u53e5\u3002\u9019\u7a2e\u65b9\u6cd5\u9069\u7528\u65bc\u55ae\u4e00\u8b8a\u91cf\u7684\u8ce6\u503c\uff1a<\/p>\n<pre><code>SET @Count = 10;<\/code><\/pre>\n<h3>2. \u4f7f\u7528 <code>SELECT<\/code> \u8a9e\u53e5<\/h3>\n<p>\u53e6\u4e00\u7a2e\u8ce6\u503c\u65b9\u6cd5\u662f\u4f7f\u7528 <code>SELECT<\/code> \u8a9e\u53e5\u3002\u9019\u7a2e\u65b9\u6cd5\u53ef\u4ee5\u540c\u6642\u70ba\u591a\u500b\u8b8a\u91cf\u8ce6\u503c\uff1a<\/p>\n<pre><code>SELECT @Count = COUNT(*) FROM Employees;<\/code><\/pre>\n<p>\u9019\u88e1\uff0c<code>COUNT(*)<\/code> \u7684\u7d50\u679c\u5c07\u88ab\u8ce6\u503c\u7d66\u8b8a\u91cf <code>@Count<\/code>\u3002<\/p>\n<h3>3. \u4f7f\u7528 <code>OUTPUT<\/code> \u5b50\u53e5<\/h3>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5728\u57f7\u884c\u63d2\u5165\u6216\u66f4\u65b0\u64cd\u4f5c\u6642\u7372\u53d6\u5f71\u97ff\u7684\u884c\u6578\u64da\u3002\u9019\u6642\u53ef\u4ee5\u4f7f\u7528 <code>OUTPUT<\/code> \u5b50\u53e5\uff1a<\/p>\n<pre><code>DECLARE @InsertedRows TABLE (ID INT);\nINSERT INTO Employees (Name) OUTPUT INSERTED.ID INTO @InsertedRows VALUES ('John Doe');<\/code><\/pre>\n<p>\u9019\u6a23\uff0c\u63d2\u5165\u7684\u884c\u7684ID\u5c07\u88ab\u5b58\u5132\u5728\u8868\u8b8a\u91cf <code>@InsertedRows<\/code> \u4e2d\u3002<\/p>\n<h2 id=\"%e8%ae%8a%e9%87%8f%e7%9a%84%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-qYORFrqDmf\">\u8b8a\u91cf\u7684\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528\u8b8a\u91cf\u6642\uff0c\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5be6\u8e10\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u548c\u6027\u80fd\uff1a<\/p>\n<ul>\n<li><strong>\u660e\u78ba\u547d\u540d\uff1a<\/strong>\u8b8a\u91cf\u540d\u7a31\u61c9\u8a72\u6e05\u6670\u4e14\u5177\u63cf\u8ff0\u6027\uff0c\u4ee5\u4fbf\u5176\u4ed6\u958b\u767c\u8005\u80fd\u5920\u7406\u89e3\u5176\u7528\u9014\u3002<\/li>\n<li><strong>\u9069\u7576\u4f7f\u7528\u6578\u64da\u985e\u578b\uff1a<\/strong>\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u985e\u578b\u53ef\u4ee5\u7bc0\u7701\u5b58\u5132\u7a7a\u9593\u4e26\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li><strong>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8ce6\u503c\uff1a<\/strong>\u5728\u4e0d\u9700\u8981\u7684\u60c5\u6cc1\u4e0b\u8ce6\u503c\u6703\u589e\u52a0\u67e5\u8a62\u7684\u8907\u96dc\u6027\uff0c\u61c9\u8a72\u907f\u514d\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-qYORFrqDmf\">\u7d50\u8ad6<\/h2>\n<p>MSSQL\u4e2d\u7684\u8b8a\u91cf\u8ce6\u503c\u6280\u5de7\u662f\u6578\u64da\u5eab\u958b\u767c\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u901a\u904e\u5408\u7406\u7684\u8b8a\u91cf\u8072\u660e\u548c\u8ce6\u503c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u67e5\u8a62\u7684\u6548\u7387\u548c\u53ef\u8b80\u6027\u3002\u7121\u8ad6\u662f\u4f7f\u7528 <code>SET<\/code> \u9084\u662f <code>SELECT<\/code>\uff0c\u9078\u64c7\u5408\u9069\u7684\u65b9\u6cd5\u5c07\u6709\u52a9\u65bc\u512a\u5316\u60a8\u7684SQL\u4ee3\u78bc\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u670d\u52d9\u6709\u8208\u8da3\uff0c\u6216\u60f3\u4e86\u89e3\u66f4\u591a\u95dc\u65bc <a href=\"https:\/\/server.hk\">\u96f2\u4f3a\u670d\u5668<\/a> \u7684\u8cc7\u8a0a\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8a73\u7d30\u8cc7\u6599\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22MSSQL\u4e2d\u7684\u8b8a\u91cf\u8ce6\u503c\u6280\u5de7\uff0c\u5b78\u7fd2\u5982\u4f55\u6709\u6548\u5730\u4f7f\u7528\u8b8a\u91cf\u63d0\u5347\u67e5\u8a62\u6548\u7387\u548c\u4ee3\u78bc\u53ef\u8b80\u6027\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-66323","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/66323","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=66323"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/66323\/revisions"}],"predecessor-version":[{"id":66324,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/66323\/revisions\/66324"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=66323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=66323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=66323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}