{"id":533,"date":"2010-10-01T03:39:47","date_gmt":"2010-10-01T03:39:47","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=533"},"modified":"2010-10-01T03:39:47","modified_gmt":"2010-10-01T03:39:47","slug":"t-sql-%e8%af%ad%e6%b3%95","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2010\/10\/01\/t-sql-%e8%af%ad%e6%b3%95\/","title":{"rendered":"T-SQL \u8bed\u6cd5"},"content":{"rendered":"<div><a href=\"http:\/\/msdn.microsoft.com\/zh-cn\/library\/ms180169.aspx\">http:\/\/msdn.microsoft.com\/zh-cn\/library\/ms180169.aspx<\/a><\/div>\n<div>?<\/div>\n<div>\u4e00\uff0cSQL\u53d8\u91cf<\/div>\n<div>?\u5c40\u90e8\u53d8\u91cf\u5fc5\u987b\u4ee5\u201c@\u201d\u5f00\u5934\uff0c\u800c\u4e14\u5fc5\u987b\u5148\u7528DECLARE\u547d\u4ee4\u8bf4\u660e\u540e\u624d\u53ef\u4f7f\u7528\uff0c\u5728Transact-SQL\u4e2d\u4e0d\u80fd\u50cf\u5728\u4e00\u822c\u7684\u7a0b\u5e8f\u8bed\u8a00\u4e2d\u4e00\u6837\u4f7f\u7528\u201c\u53d8\u91cf=\u53d8\u91cf\u503c\u201d\u6765\u7ed9\u53d8\u91cf\u8d4b\u503c\u3002\u5fc5\u987b\u4f7f\u7528SELECT\u6216SET\u547d\u4ee4\u6765\u8bbe\u5b9a\u53d8\u91cf\u7684\u503c\uff0c\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/div>\n<div>?? ?SELECT@\u5c40\u90e8\u53d8\u91cf=\u53d8\u91cf\u503c?SET @\u5c40\u90e8\u53d8\u91cf=\u53d8\u91cf\u503c<\/div>\n<div>\u3000declare@id char\uff0810\uff09\u3000\u3000select@id=\u201810010001\u2019<\/div>\n<div><span style=\"font-size: medium;\">\u5728Select\u547d\u4ee4\u67e5\u8be2\u6570\u636e\u65f6\uff0c\u5728Select\u547d\u4ee4\u4e2d\u76f4\u63a5\u5c06\u5217\u503c\u8d4b\u7ed9\u53d8\u91cf<br \/>\n<\/span><\/div>\n<p><span style=\"font-size: small; font-family: arial;\">?? ?declare@name char\uff0830\uff09@wage money<br \/>\n<\/span><span style=\"font-size: small; font-family: arial;\">?? ??? ?select@name=e_name\uff0c@wage=e_wage<br \/>\n<\/span><span style=\"font-size: small; font-family: arial;\">?? ??? ?from employee<br \/>\n<\/span><span style=\"font-size: small; font-family: arial;\">?? ??? ?where emp_id=&#8217;10010001&#8242;<br \/>\n<\/span><span style=\"font-size: small; font-family: arial;\">?? ??? ?select@name as e_name\uff0c@wage as e_wage<\/span><\/p>\n<div>?? ?<\/div>\n<div>?? ?\u5168\u5c40\u53d8\u91cf:<span style=\"font-size: small; font-family: arial;\">\u5168\u5c40\u53d8\u91cf\u4e0d\u662f\u7531\u7528\u6237\u7684\u7a0b\u5e8f\u5b9a\u4e49\u7684\uff0c\u5b83\u4eec\u662f\u5728\u670d\u52a1\u5668\u7ea7\u5b9a\u5e94\u4e49\u7684\u3002\u53ea\u80fd\u4f7f\u7528\u9884\u5148\u8bf4\u660e\u53ca\u5b9a\u4e49\u7684\u53d8\u5c40\u53d8\u91cf\u3002\u5f15\u7528\u5168\u5c40\u53d8\u91cf\u65f6\uff0c\u5fc5\u987b\u4ee5\u201c@@\u201d\u5f00\u5934\u3002\u5c40\u90e8\u53d8\u91cf\u7684\u540d\u79f0\u4e0d\u80fd\u4e0e\u5168\u5c40\u53d8\u91cf\u7684\u540d\u79f0\u76f8\u540c\u3001\u5426\u5219\u4f1a\u5728\u5e94\u7528\u4e2d\u51fa\u9519\u3002<br \/>\n<\/span><span style=\"font-size: small; font-family: arial;\"><br \/>\n<\/span><\/p>\n<h3>\u4e8c\u3001SQL\u6d41\u7a0b\u63a7\u5236\u547d\u4ee4<\/h3>\n<p>?? 1,if<\/p>\n<p>?? ?select @x=1,@y=2, @z=3<\/p>\n<p>\u3000\u3000if@x&gt;@y<\/p>\n<p>\u3000\u3000<span style=\"color: #ff0000;\">print<\/span>&#8216;x&gt;y&#8217; &#8211;\u6253\u5370\u5b57\u7b26\u4e32&#8217;x&gt;y&#8217; ? \/print \u4ee3\u8868\u8f93\u51fa\u5230\u63a7\u5236\u53f0<\/p>\n<p>\u3000\u3000else if@y&gt;@z<\/p>\n<p>\u3000\u3000print&#8217;y&gt;z&#8217;<\/p>\n<p>\u3000\u3000else print&#8217;z&gt;y&#8217;?<\/p>\n<p>?? ?2,?BEGIN\u2026END<\/p>\n<p>?? ??? ?\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>\u3000\u3000BEGIN<\/p>\n<p>\u3000\u3000&lt;\u547d\u4ee4\u884c\u6216\u7a0b\u5e8f\u5757&gt;<\/p>\n<p>\u3000\u3000END<\/p>\n<p>\u3000\u3000BEGIN\u2026END\u7528\u6765\u8bbe\u5b9a\u4e00\u4e2a\u7a0b\u5e8f\u5757\uff0c\u5c06\u5728BEGIN\u2026END\u5185\u7684\u6240\u6709\u7a0b\u5e8f\u89c6\u4e3a\u4e00\u4e2a\u5355\u5143\u6267\u884cBEGIN\u2026END\u7ecf\u5e38\u5728\u6761\u4ef6\u8bed\u53e5\uff0c\u5982IF\u2026ELSE\u4e2d\u4f7f\u7528\u3002\u5728BEGIN\u2026END\u4e2d\u53ef\u5d4c\u5957\u53e6\u5916\u7684BEGIN\u2026END\u6765\u5b9a\u4e49\u53e6\u4e00\u7a0b\u5e8f\u5757\u3002<\/p>\n<p>3 CASE<\/p>\n<p>\u3000\u3000CASE \u547d\u4ee4\u6709\u4e24\u79cd\u8bed\u53e5\u683c\u5f0f:<\/p>\n<p>\u3000\u3000CASE &lt;\u8fd0\u7b97\u5f0f&gt;<\/p>\n<p>\u3000\u3000WHEN &lt;\u8fd0\u7b97\u5f0f&gt;THEN&lt;\u8fd0\u7b97\u5f0f&gt;<\/p>\n<p>\u3000\u3000\u2026<\/p>\n<p>\u3000\u3000WHEN&lt;\u8fd0\u7b97\u5f0f&gt;THEN&lt;\u8fd0\u7b97\u5f0f&gt;<\/p>\n<p>\u3000\u3000[ELSE&lt;\u8fd0\u7b97\u5f0f&gt;]<\/p>\n<p>?se pangu<\/p>\n<p>\u3000\u3000update employee<\/p>\n<p>\u3000\u3000set e_wage =<\/p>\n<p>\u3000\u3000case<\/p>\n<p>\u3000\u3000when job_level = \u20191\u2019 then e_wage*1.08<\/p>\n<p>\u3000\u3000when job_level = \u20192\u2019 then e_wage*1.07<\/p>\n<p>\u3000\u3000when job_level = \u20193\u2019 then e_wage*1.06<\/p>\n<p>\u3000\u3000else e_wage*1.05<\/p>\n<p>\u3000\u3000end<\/p>\n<p>4 WHILE\u2026CONTINUE\u2026BREAK<\/p>\n<p>\u3000\u3000\u5176\u8bed\u6cd5\u5982\u4e0b:<\/p>\n<p>\u3000\u3000WHILE &lt;\u6761\u4ef6\u8868\u8fbe\u5f0f&gt;<\/p>\n<p>\u3000\u3000BEGIN<\/p>\n<p>\u3000\u3000&lt;\u547d\u4ee4\u884c\u6216\u7a0b\u5e8f\u5757&gt;<\/p>\n<p>\u3000\u3000[BREAK]<\/p>\n<p>\u3000\u3000[CONTINUE]<\/p>\n<p>\u3000\u3000[\u547d\u4ee4\u884c\u6216\u7a0b\u5e8f\u5757]<\/p>\n<p>\u3000\u3000END<\/p>\n<p>\u3000\u3000WHILE \u547d\u4ee4\u5728\u8bbe\u5b9a\u7684\u6761\u4ef6\u6210\u7acb\u65f6\u4f1a\u91cd\u590d\u6267\u884c\u547d\u4ee4\u884c\u6216\u7a0b\u5e8f\u5757\u3002CONTINUE\u547d\u4ee4\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u8df3\u8fc7CONTINUE \u547d\u4ee4\u4e4b\u540e\u7684\u8bed\u53e5\uff0c\u56de\u5230WHILE \u5faa\u73af\u7684\u7b2c\u4e00\u884c\u547d\u4ee4\u3002BREAK \u547d\u4ee4\u5219\u8ba9\u7a0b\u5e8f\u5b8c\u5168\u8df3\u51fa\u5faa\u73af\uff0c\u7ed3\u675fWHILE \u547d\u4ee4\u7684\u6267\u884c\u3002WHILE \u8bed\u53e5\u4e5f\u53ef\u4ee5\u5d4c\u5957\u3002<\/p>\n<p>5,\u5176\u5b83\u547d\u4ee4\u3002<\/p>\n<p>?\uff081\uff09PRINT<\/p>\n<p>\u3000\u3000\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>\u3000\u3000PRINT &#8216;any ASCII text&#8217; | @local_variable | @@FUNCTION | string_expression<\/p>\n<p>\u3000\u3000PRINT \u547d\u4ee4\u5411\u5ba2\u6237\u7aef\u8fd4\u56de\u4e00\u4e2a\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u4fe1\u606f\uff0c\u5373\u663e\u793a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff08\u6700\u957f\u4e3a255\u4e2a\u5b57\u7b26\uff09\u3001\u5c40\u90e8\u53d8\u91cf\u6216\u5168\u5c40\u53d8\u91cf\u3002\u5982\u679c\u53d8\u91cf\u503c\u4e0d\u662f\u5b57\u7b26\u4e32\u7684\u8bdd\uff0c\u5fc5\u987b\u5148\u7528\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u51fd\u6570CONVERT\uff08\uff09\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u5176\u4e2d\uff0cstring_expression \u662f\u53ef\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u8868\u8fbe\u5f0f\u3002\u8868\u8fbe\u5f0f\u7684\u957f\u5ea6\u53ef\u4ee5\u8d85\u8fc78000 \u4e2a\u5b57\u7b26\uff0c\u4f46\u8d85\u8fc78000 \u7684\u5b57\u7b26\u5c06\u4e0d\u4f1a\u663e\u793a\u3002<\/p>\n<p>?(2)\u5f97\u5230\u65e5\u671f<strong>? <\/strong><span style=\"color: #0000ff;\">SELECT GETDATE()??? <\/span>SELECT CONVERT(varchar(100), GETDATE(), 20) : <span style=\"color: #000000;\">2009-06-27 09:24:51<br \/>\n<\/span><\/p>\n<p>6,\u4f8b\u5b50.<\/p>\n<p>?\u904d\u5386\u5143\u7d20<\/p>\n<div>\n<p>DECLARE @vcum1 char(255)<br \/>\nDECLARE @vcum2 char(255)<br \/>\nDECLARE @vcum3 char(255)<\/p><\/div>\n<\/div>\n<div>DECLARE? cs1 CURSOR FOR SELECT * FROM city<br \/>\nOPEN cs1<br \/>\nFETCH cs1 INTO @vcum1, @vcum2,@vcum3<br \/>\nWHILE @@FETCH_STATUS = 0<br \/>\nBEGIN<br \/>\n?PRINT @vcum1<br \/>\n?FETCH NEXT FROM cs1 INTO @vcum1, @vcum2,@vcum3<br \/>\n?<br \/>\nEND<br \/>\nCLOSE cs1<br \/>\nDEALLOCATE cs1\u6ce8\u610f:null\u662f\u4e2a\u975e\u5e38\u7279\u6b8a\u7684\u503c\uff0c\u4ec0\u4e48\u503c\u4e0enull\u7684\u8fd0\u7b97\u7ed3\u679c\u90fd\u662fnull\uff0c\u5e38\u5e38\u5bfc\u81f4\u8bed\u53e5\u9519\u8bef\u3002\u6240\u4ee5\u5728\u8bbe\u8ba1\u5e93\u8868\u7ed3\u6784\u65f6\uff0c\u5e38\u5e38\u8bbe\u7f6edefault\u503c\uff0c\u907f\u514dnull\u7684\u51fa\u73b0\u3002\u4f46\u8fd9\u6837\u4f1a\u589e\u5927\u6570\u636e\u6587\u4ef6\u7684\u4f53\u79ef\uff0c\u6d6a\u8d39\u8d44\u6e90\u3002\u5f53\u8868\u4e2d\u6570\u503c\u5f88\u7a00\u758f\u65f6\uff0c\u8fd9\u79cd\u6d6a\u8d39\u662f\u975e\u5e38\u60ca\u4eba\u7684\u3002<\/p>\n<p>\u5224\u65ad\u662f\u5426\u7b49\u4e8enull\uff0c\u4e0d\u80fd\u7528=null\u548c&lt;&gt;null\uff0c\u8981\u7528 is null \u548c is not null \u3002<\/p>\n<p>\u8bbe\u7f6e\u5b57\u6bb5\u503c\u4e3anull\uff0c\u53ef\u4ee5\u7528 update XX set YY=null\u3002null\u5916\u9762\u4e0d\u8981\u52a0\u5f15\u53f7\u3002<\/p>\n<p>\u63d2\u5165\u65b0\u8bb0\u5f55\u7528 insert XXX (YYY) values(null)\u3002null\u5916\u9762\u4e0d\u8981\u52a0\u5f15\u53f7\u3002<\/p>\n<p>\u7279\u522b\u8981\u6ce8\u610fnull\u4e0e\u201dNULL\u201d\u3001\u201dnull\u201d\u7684\u533a\u522b\u3002\u5728SQL Server\u67e5\u8be2\u5206\u6790\u5668\u4e2d\uff0cnull\u4e0e\u201dNULL\u201d\u7684\u663e\u793a\u5b8c\u5168\u76f8\u540c\uff0c\u975e\u5e38\u5bb9\u6613\u6df7\u6dc6\uff01<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/msdn.microsoft.com\/zh-cn\/library\/ms180169.aspx ? [&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-533","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\/533","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=533"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/533\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}