{"id":143735,"date":"2024-11-01T00:46:29","date_gmt":"2024-10-31T16:46:29","guid":{"rendered":"https:\/\/server.hk\/cnblog\/143735\/"},"modified":"2024-11-01T00:46:30","modified_gmt":"2024-10-31T16:46:30","slug":"sql-%e8%ae%8a%e9%87%8f%e5%ae%9a%e7%be%a9%e8%b3%a6%e5%80%bc%e5%8f%8a%e8%aa%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/143735\/","title":{"rendered":"SQL \u8b8a\u91cf\u5b9a\u7fa9\u8ce6\u503c\u53ca\u8abf\u7528"},"content":{"rendered":"<h1 id=\"sql-%e8%ae%8a%e9%87%8f%e5%ae%9a%e7%be%a9%e8%b3%a6%e5%80%bc%e5%8f%8a%e8%aa%bf%e7%94%a8-nxsskgsZiG\">SQL \u8b8a\u91cf\u5b9a\u7fa9\u8ce6\u503c\u53ca\u8abf\u7528<\/h1>\n<p>\u5728\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\u4e2d\uff0cSQL\uff08\u7d50\u69cb\u5316\u67e5\u8a62\u8a9e\u8a00\uff09\u662f\u4e00\u7a2e\u7528\u65bc\u67e5\u8a62\u548c\u64cd\u4f5c\u8cc7\u6599\u7684\u6a19\u6e96\u8a9e\u8a00\u3002SQL \u8b8a\u91cf\u7684\u4f7f\u7528\u53ef\u4ee5\u63d0\u9ad8\u67e5\u8a62\u7684\u9748\u6d3b\u6027\u548c\u53ef\u8b80\u6027\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u91cd\u8907\u4f7f\u7528\u67d0\u4e9b\u503c\u6216\u9032\u884c\u8907\u96dc\u8a08\u7b97\u7684\u60c5\u6cc1\u4e0b\u3002\u672c\u6587\u5c07\u63a2\u8a0e SQL \u8b8a\u91cf\u7684\u5b9a\u7fa9\u3001\u8ce6\u503c\u53ca\u8abf\u7528\u65b9\u6cd5\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u7bc4\u4f8b\u4ee5\u5e6b\u52a9\u7406\u89e3\u3002<\/p>\n<h2 id=\"sql-%e8%ae%8a%e9%87%8f%e7%9a%84%e5%ae%9a%e7%be%a9-nxsskgsZiG\">SQL \u8b8a\u91cf\u7684\u5b9a\u7fa9<\/h2>\n<p>\u5728 SQL \u4e2d\uff0c\u8b8a\u91cf\u662f\u4e00\u7a2e\u7528\u65bc\u5b58\u5132\u8cc7\u6599\u7684\u547d\u540d\u5132\u5b58\u5340\u3002\u8b8a\u91cf\u53ef\u4ee5\u5728 SQL \u8173\u672c\u6216\u5b58\u5132\u904e\u7a0b\u4e2d\u4f7f\u7528\uff0c\u4e26\u4e14\u53ef\u4ee5\u5728\u57f7\u884c\u671f\u9593\u6539\u8b8a\u5176\u503c\u3002\u8b8a\u91cf\u7684\u5b9a\u7fa9\u901a\u5e38\u4f7f\u7528 <code>DECLARE<\/code> \u8a9e\u53e5\uff0c\u5f8c\u9762\u8ddf\u8457\u8b8a\u91cf\u7684\u540d\u7a31\u548c\u6578\u64da\u985e\u578b\u3002<\/p>\n<pre><code>DECLARE @myVariable INT;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba <code>@myVariable<\/code> \u7684\u6574\u6578\u8b8a\u91cf\u3002\u8b8a\u91cf\u540d\u7a31\u901a\u5e38\u4ee5 <code>@<\/code> \u958b\u982d\uff0c\u4ee5\u4fbf\u8207\u5176\u4ed6\u8b58\u5225\u7b26\u5340\u5206\u3002<\/p>\n<h2 id=\"sql-%e8%ae%8a%e9%87%8f%e7%9a%84%e8%b3%a6%e5%80%bc-nxsskgsZiG\">SQL \u8b8a\u91cf\u7684\u8ce6\u503c<\/h2>\n<p>\u8b8a\u91cf\u5b9a\u7fa9\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>SET<\/code> \u6216 <code>SELECT<\/code> \u8a9e\u53e5\u70ba\u5176\u8ce6\u503c\u3002\u4ee5\u4e0b\u662f\u5169\u7a2e\u8ce6\u503c\u65b9\u6cd5\u7684\u7bc4\u4f8b\uff1a<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8-set-%e8%aa%9e%e5%8f%a5%e8%b3%a6%e5%80%bc-nxsskgsZiG\">\u4f7f\u7528 SET \u8a9e\u53e5\u8ce6\u503c<\/h3>\n<pre><code>SET @myVariable = 10;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5c07 <code>@myVariable<\/code> \u7684\u503c\u8a2d\u7f6e\u70ba 10\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8-select-%e8%aa%9e%e5%8f%a5%e8%b3%a6%e5%80%bc-nxsskgsZiG\">\u4f7f\u7528 SELECT \u8a9e\u53e5\u8ce6\u503c<\/h3>\n<pre><code>SELECT @myVariable = COUNT(*) FROM Employees;<\/code><\/pre>\n<p>\u9019\u88e1\uff0c\u6211\u5011\u4f7f\u7528 <code>SELECT<\/code> \u8a9e\u53e5\u5c07 <code>Employees<\/code> \u8868\u4e2d\u7684\u8a18\u9304\u6578\u91cf\u8ce6\u503c\u7d66 <code>@myVariable<\/code>\u3002<\/p>\n<h2 id=\"sql-%e8%ae%8a%e9%87%8f%e7%9a%84%e8%aa%bf%e7%94%a8-nxsskgsZiG\">SQL \u8b8a\u91cf\u7684\u8abf\u7528<\/h2>\n<p>\u4e00\u65e6\u8b8a\u91cf\u88ab\u8ce6\u503c\uff0c\u5c31\u53ef\u4ee5\u5728 SQL \u67e5\u8a62\u4e2d\u8abf\u7528\u5b83\u3002\u9019\u4f7f\u5f97\u67e5\u8a62\u66f4\u52a0\u9748\u6d3b\u548c\u53ef\u8b80\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8abf\u7528\u8b8a\u91cf\u7684\u7bc4\u4f8b\uff1a<\/p>\n<h3 id=\"%e5%9c%a8%e6%9f%a5%e8%a9%a2%e4%b8%ad%e4%bd%bf%e7%94%a8%e8%ae%8a%e9%87%8f-nxsskgsZiG\">\u5728\u67e5\u8a62\u4e2d\u4f7f\u7528\u8b8a\u91cf<\/h3>\n<pre><code>SELECT * FROM Employees WHERE EmployeeID = @myVariable;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>@myVariable<\/code> \u4f5c\u70ba\u67e5\u8a62\u689d\u4ef6\uff0c\u5f9e <code>Employees<\/code> \u8868\u4e2d\u9078\u64c7\u7279\u5b9a\u7684\u54e1\u5de5\u3002<\/p>\n<h3 id=\"%e5%9c%a8%e8%a8%88%e7%ae%97%e4%b8%ad%e4%bd%bf%e7%94%a8%e8%ae%8a%e9%87%8f-nxsskgsZiG\">\u5728\u8a08\u7b97\u4e2d\u4f7f\u7528\u8b8a\u91cf<\/h3>\n<pre><code>DECLARE @totalSalary INT;\nSELECT @totalSalary = SUM(Salary) FROM Employees;\nSELECT @totalSalary AS TotalSalary;<\/code><\/pre>\n<p>\u9019\u88e1\uff0c\u6211\u5011\u9996\u5148\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba <code>@totalSalary<\/code> \u7684\u8b8a\u91cf\uff0c\u7136\u5f8c\u8a08\u7b97 <code>Employees<\/code> \u8868\u4e2d\u6240\u6709\u54e1\u5de5\u7684\u85aa\u8cc7\u7e3d\u548c\uff0c\u4e26\u5c07\u7d50\u679c\u5b58\u5132\u5728 <code>@totalSalary<\/code> \u4e2d\u3002\u6700\u5f8c\uff0c\u6211\u5011\u9078\u64c7\u986f\u793a\u9019\u500b\u7e3d\u85aa\u8cc7\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-nxsskgsZiG\">\u7d50\u8ad6<\/h2>\n<p>SQL \u8b8a\u91cf\u7684\u5b9a\u7fa9\u3001\u8ce6\u503c\u53ca\u8abf\u7528\u662f\u8cc7\u6599\u5eab\u64cd\u4f5c\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u90e8\u5206\u3002\u900f\u904e\u4f7f\u7528\u8b8a\u91cf\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u63d0\u9ad8\u67e5\u8a62\u7684\u9748\u6d3b\u6027\u548c\u53ef\u8b80\u6027\uff0c\u4e26\u80fd\u5920\u66f4\u6709\u6548\u5730\u8655\u7406\u8907\u96dc\u7684\u8cc7\u6599\u64cd\u4f5c\u3002\u7121\u8ad6\u662f\u5728\u7c21\u55ae\u7684\u67e5\u8a62\u9084\u662f\u8907\u96dc\u7684\u5b58\u5132\u904e\u7a0b\u4e2d\uff0c\u8b8a\u91cf\u90fd\u80fd\u767c\u63ee\u91cd\u8981\u4f5c\u7528\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc\u8cc7\u6599\u5eab\u7ba1\u7406\u6216\u5176\u4ed6\u76f8\u95dc\u6280\u8853\u6709\u8208\u8da3\uff0c\u4e26\u5e0c\u671b\u5c0b\u627e\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3SQL\u4e2d\u8b8a\u91cf\u7684\u5b9a\u7fa9\u3001\u8ce6\u503c\u53ca\u8abf\u7528\u65b9\u6cd5\uff0c\u63d0\u5347\u6578\u64da\u5eab\u64cd\u4f5c\u6548\u7387\uff0c\u7c21\u5316\u67e5\u8a62\u548c\u6578\u64da\u8655\u7406\u904e\u7a0b\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-143735","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/143735","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=143735"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/143735\/revisions"}],"predecessor-version":[{"id":143736,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/143735\/revisions\/143736"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=143735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=143735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=143735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}