{"id":1683,"date":"2011-04-08T00:39:47","date_gmt":"2011-04-08T00:39:47","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=1683"},"modified":"2013-08-01T14:04:08","modified_gmt":"2013-08-01T14:04:08","slug":"%e7%94%a8update%e8%b5%8b%e5%80%bc","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2011\/04\/08\/%e7%94%a8update%e8%b5%8b%e5%80%bc\/","title":{"rendered":"\u805a\u5408\u51fd\u6570 \u7528Update\u3001Select\u8d4b\u503c"},"content":{"rendered":"<pre class=\"brush:sql;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;\">\r\n1,Update\u4e0d\u4ec5\u53ef\u4ee5\u7ed9\u5b57\u6bb5\u8d4b\u503c\uff0c\u76f4\u63a5\u7ed9\u53d8\u91cf\u8d4b\u503c\u4e5f\u884c\r\nDECLARE @str VARCHAR(50)\r\nUPDATE AA\r\nSET aa.c3 = &lsquo;hello&rsquo;,@str = c3\r\nFROM tmp aa\r\nWHERE aa.c2 = &lsquo;aa&rsquo;\r\n\r\n2, \u8d4b\u503c\u7684\u4fdd\u7559\u503c\uff0c\u5373\u6267\u884c SELECT ,UPDATE\u7ed9\u53d8\u91cf\u8d4b\u503c\uff0c\u82e5\u6ca1\u6709\u7ed3\u679c\uff0c\u5219\u4fdd\u7559\u539f\u503c\u3002\u82e5\u6267\u884c\u7684\u662fcount,\u5219\u53d8\u91cf\u8d4b\u503c\u4e3a0\uff0c\u82e5\u6267\u884c\u7684\u662fsum,\u5219\u53d8\u91cf\u8d4b\u503c\u4e3a\u7a7a\uff0c\u5373\u5bf9\u4e8e\u805a\u5408\u51fd\u6570\uff0c\u9664count\u8fd4\u56de\u4e3a0\u5916\uff0c\u5176\u5b83\u5982sum, max\u5747\u8fd4\u56deNULL\u3002\r\n\u6267\u884c SELECT ,UPDATE\u7ed9\u53d8\u91cf\u8d4b\u503c\uff0c\u82e5\u6ca1\u6709\u7ed3\u679c\uff0c\u5219\u4fdd\u7559\u539f\u503c\uff0c\u5bf9\u4e8e\u805a\u5408\u51fd\u6570\uff0c\u90fd\u662f\u8fd4\u56de\u6709\u503c\u7684\uff0c\u53ea\u662f\u5bf9\u4e8ecount,\u82e5\u6ca1\u6709\u7ed3\u679c\uff0c\u90a3\u4e48\u8ba1\u6570\u4e3a0\uff0c\u5373\u8fd4\u56de\u503c\u662f0\uff0c\u800c\u4e8esum,max\u82e5\u6ca1\u6709\u7ed3\u679c\uff0c\u90a3\u4e48\u805a\u5408\u7684\u7ed3\u679c\u662fnull,null\u4e5f\u4f1a\u8fd4\u56de\uff0c\u8986\u76d6\u539f\u53d8\u91cf\u503c\u3002\r\n\r\n3\uff0c\u805a\u5408\u51fd\u6570\u4e2d\uff0c SUM\/AVG\/COUNT\u4e2d\u7684NULL\u4f1a\u88ab\u5ffd\u7565\uff0c\u5bf9sum,avg,\u8fd8\u6ca1\u4ec0\u4e48\u5173\u7cfb\uff0c\u4f46\u8981\u6ce8\u610f\u7684\u662fCOUNT,NULL\u88ab\u5ffd\u7565\u610f\u5473\u7740\u82e5count(\u5b57\u6bb5)\uff0c\u6b64\u5b57\u6bb5\u6709\u4e00\u884c\u4e3aNULL,\u5219\u6570\u91cf\u4f1a\u5c11\u4e00\u884c\uff0c\u4ed6\u5ffd\u7565\u7684NULL\u5c31\u662f()\u91cc\u7684\u5b57\u6bb5\uff0c\u82e5\u4e3a\u5e38\u91cf\u6216*\uff0c\u90fd\u4e0d\u662fNULL,\u4e5f\u5c31\u6ca1\u6709\u5ffd\u7565\u53ef\u8a00\u3002\r\n\r\ndrop table t_count\r\ncreate table t_count\r\n(\r\nc1 varchar(10) null,\r\nc2 varchar(10) null\r\n)\r\n\r\ninsert into t_count values(null,null)\r\ninsert into t_count values(&#39;a&#39;,&#39;b&#39;)\r\ninsert into t_count values(&#39;a&#39;,&#39;b&#39;)\r\ninsert into t_count values(&#39;c&#39;,&#39;d&#39;)\r\ninsert into t_count values(&#39;c&#39;,&#39;d&#39;)\r\n\r\nselect COUNT(1) from t_count --5\r\nselect COUNT(c1) from t_count --4\r\nselect COUNT(distinct c1) from t_count --2\r\n\r\n--\u6b63\u5e38\r\nselect count(*) from (select distinct c1,c2 from t_count) t --3\r\n--\u6709NULL\u53c2\u4e0e\u4e86\u8fd0\u7b97\uff0c\u6240\u4ee5\u8868\u8fbe\u5f0f\u503c\u4e3aNULL\r\nselect count(distinct c1+c2) from t_count --2\r\n\r\n4\uff0c\u5f53\u7528SELECT,UPDATE\u8d4b\u503c\u65f6\uff0c\u52a0\u4e2aTOP,\u627e\u5230\u5373\u8fd4\u56de\uff0c\u9632\u6b62\u904d\u5386\u6240\u6709\u884c\uff01\r\n\u6ce8\uff1aselect top \u4e0e select\u76f8\u540c\u70b9\u90fd\u4f1a\u5c06\u6240\u6709\u6ee1\u8db3\u6761\u4ef6\u7684\u6570\u636e\u7b5b\u9009\u51fa\u6765\u7136\u540e\u8fdb\u884c\u8d4b\u503c\uff0ctop\u5e76\u4e0d\u662f\u7b5b\u9009\u5230\u4e00\u6761\u5c31\u8fd4\u56de\uff0c\u82e5\u662f\u5c06\u6709\u6ee1\u8db3\u6761\u4ef6\u7684\u6570\u636e\u7b5b\u9009\u51fa\u6765\u540e\uff0c\u8d4b\u503c\u4e00\u4e2a\u5373\u8fd4\u56de\uff0c\u6545\u8d4b\u503cselect top\u4e0d\u4f1a\u6bd4select \u6027\u80fd\u63d0\u9ad8\u591a\u5c11\u3002\r\nDECLARE @username VARCHAR(50),@updateusername VARCHAR(50),\r\n@count INT,@sum INT,@max int\r\nSET @username = &lsquo;tt&rsquo;\r\nSET @updateusername = &lsquo;cc&rsquo;\r\nSET @count = 22\r\nSET @sum = 33\r\nSET @max = 44\r\nSELECT @username = f.UserName\r\nFROM frmuser f\r\nWHERE 1=2\r\nUPDATE f\r\nSET @updateusername = f.username\r\nFROM frmuser f\r\nWHERE 1=2\r\nSELECT @count= COUNT(1)\r\nFROM frmuser f\r\nWHERE 1=2\r\nSELECT @sum= SUM(f.ID)\r\nFROM frmuser f\r\nWHERE 1=2\r\nSELECT @max= max(f.ID)\r\nFROM frmuser f\r\nWHERE 1=2\r\nSELECT @username &ndash;tt\r\nSELECT @updateusername &ndash;cc\r\nSELECT @count &ndash;0\r\nSELECT @sum &ndash;null\r\nSELECT @max &ndash;null\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1,Update\u4e0d\u4ec5\u53ef\u4ee5\u7ed9\u5b57\u6bb5\u8d4b\u503c\uff0c\u76f4\u63a5\u7ed9\u53d8\u91cf\u8d4b\u503c\u4e5f\u884c DECLARE @str VARCHAR(50) UP [&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-1683","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\/1683","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=1683"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/1683\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=1683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=1683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=1683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}