{"id":309,"date":"2022-06-20T15:37:00","date_gmt":"2022-06-20T07:37:00","guid":{"rendered":"https:\/\/frogvps.com\/?p=309"},"modified":"2022-06-26T12:51:13","modified_gmt":"2022-06-26T04:51:13","slug":"mysql%e7%9a%84%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b%e7%9a%84%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/frogvps.com\/?p=309","title":{"rendered":"MYSQL\u5b58\u50a8\u8fc7\u7a0b\u7684\u7528\u6cd5"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u5b58\u50a8\u8fc7\u7a0b<\/h2>\n\n\n\n<p>\u5b58\u50a8\u8fc7\u7a0b\uff08PROCEDURE\uff09\u662f\u591a\u8868SQL\u7684\u8bed\u53e5\u7684\u96c6\u5408\uff0c\u53ef\u4ee5\u72ec\u7acb\u6267\u884c\u3002<\/p>\n\n\n\n<p><strong>\u5b58\u50a8\u8fc7\u7a0b\u7684\u4f18\u52bf\uff1a<\/strong><\/p>\n\n\n\n<p>\u5b58\u50a8\u8fc7\u7a0b\u628a\u7ecf\u5e38\u4f7f\u7528\u7684SQL\u8bed\u53e5\u6216\u4e1a\u52a1\u903b\u8f91\u5c01\u88c5\u8d77\u6765\uff0c\u9884\u7f16\u8bd1\u4fdd\u5b58\u5728\u6570\u636e\u5e93\u4e2d\u3002\u5f53\u9700\u8981\u65f6\u53ef\u4ee5\u4ece\u6570\u636e\u5e93\u4e2d\u76f4\u63a5\u8c03\u7528\uff0c\u7701\u53bb\u4e86\u7f16\u8bd1\u7684\u8fc7\u7a0b\uff0c\u63d0\u9ad8\u4e86\u8fd0\u884c\u901f\u5ea6\uff0c\u540c\u65f6\u964d\u4f4e\u7f51\u7edc\u6570\u636e\u4f20\u8f93\u91cf\u3002<\/p>\n\n\n\n<p><strong>\u5b58\u50a8\u8fc7\u7a0b\u4e0e\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u533a\u522b\uff1a<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u5b58\u50a8\u8fc7\u7a0b\u5b9e\u73b0\u7684\u8fc7\u7a0b\u8981\u590d\u6742\u4e00\u4e9b\uff0c\u800c\u51fd\u6570\u7684\u9488\u5bf9\u6027\u8f83\u5f3a\u3002<\/li><li>\u5b58\u50a8\u8fc7\u7a0b\u53ef\u4ee5\u6709\u591a\u4e2a\u8fd4\u56de\u503c\uff0c\u800c\u81ea\u5b9a\u4e49\u51fd\u6570\u53ea\u6709\u4e00\u4e2a\u8fd4\u56de\u503c\u3002<\/li><li>\u5b58\u50a8\u8fc7\u7a0b\u4e00\u822c\u53ef\u72ec\u7acb\u6267\u884c\uff0c\u800c\u51fd\u6570\u5f80\u5f80\u662f\u4f5c\u4e3a\u5176\u4ed6SQL\u8bed\u53e5\u7684\u4e00\u90e8\u5206\u6765\u4f7f\u7528\u3002<\/li><li>\u65e0\u53c2\u6570\u7684\u5b58\u50a8\u8fc7\u7a0b\u6267\u884c\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u4e0d\u52a0()\uff0c\u51fd\u6570\u5fc5\u987b\u52a0()\u3002<\/li><\/ol>\n\n\n\n<p><strong>MYSQL\u521b\u5efa\u5b58\u50a8\u8fc7\u7a0b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE PROCEDURE sp_name (&#91; proc_parameter &#91;,proc_parameter ...]])\nroutime_body\nproc_parameter : &#91;IN|OUT|INOUT] parameter_name type<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2dIN\u8868\u793a\u8f93\u5165\u53c2\u6570\uff0cOUT\u8868\u793a\u8f93\u51fa\u53c2\u6570\uff0cINOUT\u8868\u793a\u65e2\u53ef\u4ee5\u8f93\u5165\u4e5f\u53ef\u4ee5\u8f93\u51fa<\/p>\n\n\n\n<p>param_name\u8868\u793a\u53c2\u6570\u540d\u79f0<\/p>\n\n\n\n<p>type\u8868\u793a\u53c2\u6570\u7684\u7c7b\u578b<\/p>\n\n\n\n<p><strong>\u67e5\u770b\u5b58\u50a8\u8fc7\u7a0b\u5217\u8868<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW PROCEDURE STATUS;<\/code><\/pre>\n\n\n\n<p><strong>\u67e5\u770b\u5b58\u50a8\u8fc7\u7a0b\u5b9a\u4e49<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW CREATE PROCEDURE sp_name;<\/code><\/pre>\n\n\n\n<p><strong>\u8c03\u7528\u5b58\u50a8\u8fc7\u7a0b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CALL sp_name (&#91; proc_parameter &#91;,proc_parameter ...]]);\n#\u65e0\u53c2\u6570\u65f6,\u53ef\u4ee5\u7701\u7565\"()\"\u3002\u6709\u53c2\u6570\u65f6,\u4e0d\u53ef\u7701\u7565\"()\"\u3002<\/code><\/pre>\n\n\n\n<p><strong>\u5b58\u50a8\u8fc7\u7a0b\u4fee\u6539<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER\u8bed\u53e5\u4fee\u6539\u5b58\u50a8\u8fc7\u7a0b\u53ea\u80fd\u4fee\u6539\u5b58\u50a8\u8fc7\u7a0b\u7684\u6ce8\u91ca\u7b49\u65e0\u5173\u7d27\u8981\u7684\u4e1c\u897f\uff0c\u4e0d\u80fd\u4fee\u6539\u5b58\u50a8\u8fc7\u7a0b\u4f53\uff0c\u6240\u4ee5\u8981\u4fee\u6539\u5b58\u50a8\u8fc7\u7a0b\uff0c\u65b9\u6cd5\u5c31\u662f\u5220\u9664\u91cd\u5efa\u3002<\/code><\/pre>\n\n\n\n<p><strong>\u5220\u9664\u5b58\u50a8\u8fc7\u7a0b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DROP PROCEDURE &#91;IF EXISTS] sp_name;<\/code><\/pre>\n\n\n\n<p><strong>\u8303\u4f8b1\uff1a\u521b\u5efa\u65e0\u53c2\u5b58\u50a8\u8fc7\u7a0b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>delimiter \/\/\nCREATE PROCEDURE showTime()\nBEGIN\nSELECT now();\nEND\/\/\ndelimiter ;\n\nCALL showTime;<\/code><\/pre>\n\n\n\n<p><strong>\u8303\u4f8b2\uff1a\u521b\u5efa\u542b\u53c2\u5b58\u50a8\u8fc7\u7a0b\uff0c\u53ea\u6709\u4e00\u4e2aIN\u53c2\u6570<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>delimiter \/\/\nCREATE PROCEDURE selectById(IN suid SMALLINT UNSIGNED)\nBEGIN\nSELECT * FROM students WHERE id = suid;\nEND\/\/\ndelimiter ;\n\nCALL selectById(2);<\/code><\/pre>\n\n\n\n<p><strong>\u8303\u4f8b3\uff1a\u8ba1\u7b971+2+3..+n\u7684\u503c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u666e\u901a\u53d8\u91cf\u53ea\u5728\u5f53\u524d\u4f1a\u8bdd\u4e2d\u6709\u6548\uff0c\u53ef\u7528@var_name\u5f15\u7528\ndelimiter \/\/\nCREATE PROCEDURE dorepeat(n INT)\nBEGIN\nSET @i = 0;\nSET @sum = 0;\nREPEAT SET @sum = @sum+@i;\nSET @i = @i + 1;\nUNTIL @i &gt; n END REPEAT;\nEND\/\/\ndelimiter ;\n\nCALL dorepeat(100);\nSELECT @sum;<\/code><\/pre>\n\n\n\n<p><strong>\u8303\u4f8b4: \u521b\u5efa\u542b\u53c2\u5b58\u50a8\u8fc7\u7a0b,\u5305\u542bIN\u53c2\u6570\u548cOUT\u53c2\u6570<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#row_count() \u662f\u7cfb\u7edf\u5185\u7f6e\u51fd\u6570\uff0c\u5b58\u653e\u524d\u4e00\u6761SQL\u8bed\u53e5\u4fee\u6539\u8fc7\u7684\u8868\u7684\u8bb0\u5f55\u6570\u3002\ndelimiter \/\/\nCREATE PROCEDURE deleteById(IN suid SMALLINT UNSIGNED, OUT num SMALLINT UNSIGNED)\nBEGIN\nDELETE FROM students WHERE id &gt;= suid;\nSELECT row_count() into num;\nEND\/\/\ndelimiter ;\n\nCALL deleteById(20,@Line);\nSELECT @Line;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6d41\u7a0b\u63a7\u5236<\/h2>\n\n\n\n<p>\u5b58\u50a8\u8fc7\u7a0b\u548c\u51fd\u6570\u4e2d\u53ef\u4ee5\u4f7f\u7528\u6d41\u7a0b\u63a7\u5236\u6765\u63a7\u5236\u8bed\u53e5\u7684\u6267\u884c\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>IF\uff1a\u7528\u6765\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u3002\u6839\u636e\u662f\u5426\u6ee1\u8db3\u6761\u4ef6\uff0c\u6267\u884c\u4e0d\u540c\u8bed\u53e5\u3002<\/li><li>CASE\uff1a\u7528\u6765\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\uff0c\u53ef\u5b9e\u73b0\u6bd4IF\u8bed\u53e5\u66f4\u590d\u6742\u7684\u6761\u4ef6\u5224\u65ad\u3002<\/li><li>LOOP\uff1a\u91cd\u590d\u6267\u884c\u7279\u5b9a\u7684\u8bed\u53e5\uff0c\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u5faa\u73af\u3002<\/li><li>LEAVE\uff1a\u7528\u4e8e\u8df3\u51fa\u5faa\u73af\u63a7\u5236\uff0c\u76f8\u5f53\u4e8eSHELL\u4e2dbreak\u3002<\/li><li>ITERATE\uff1a\u8df3\u51fa\u672c\u6b21\u5faa\u73af\uff0c\u7136\u540e\u76f4\u63a5\u8fdb\u5165\u4e0b\u4e00\u6b21\u5faa\u73af\uff0c\u76f8\u5f53\u4e8eSHELL\u4e2dcontinue\u3002<\/li><li>REPEAT\uff1a\u6709\u6761\u4ef6\u63a7\u5236\u7684\u5faa\u73af\u8bed\u53e5\u3002\u5f53\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\uff0c\u5c31\u4f1a\u8df3\u51fa\u5faa\u73af\u8bed\u53e5\u3002<\/li><li>WHILE\uff1a\u6709\u6761\u4ef6\u63a7\u5236\u7684\u5faa\u73af\u8bed\u53e5\u3002<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5b58\u50a8\u8fc7\u7a0b \u5b58\u50a8\u8fc7\u7a0b\uff08PROCEDURE\uff09\u662f\u591a\u8868SQL\u7684\u8bed\u53e5\u7684\u96c6\u5408\uff0c\u53ef\u4ee5\u72ec\u7acb\u6267\u884c\u3002 \u5b58\u50a8\u8fc7\u7a0b\u7684\u4f18\u52bf\uff1a \u5b58\u50a8\u8fc7\u7a0b\u628a\u7ecf\u5e38\u4f7f\u7528\u7684SQL\u8bed\u53e5\u6216\u4e1a\u52a1\u903b\u8f91\u5c01\u88c5\u8d77\u6765\uff0c\u9884\u7f16\u8bd1\u4fdd\u5b58\u5728\u6570\u636e\u5e93\u4e2d\u3002\u5f53\u9700\u8981\u65f6\u53ef\u4ee5\u4ece\u6570\u636e\u5e93\u4e2d\u76f4\u63a5\u8c03\u7528\uff0c\u7701\u53bb\u4e86\u7f16\u8bd1\u7684\u8fc7\u7a0b\uff0c\u63d0\u9ad8\u4e86\u8fd0\u884c\u901f\u5ea6\uff0c\u540c&#8230;<\/p>\n","protected":false},"author":1,"featured_media":283,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[36],"topic":[],"class_list":["post-309","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-35","tag-mysql"],"_links":{"self":[{"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/posts\/309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=309"}],"version-history":[{"count":5,"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/posts\/309\/revisions"}],"predecessor-version":[{"id":355,"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/posts\/309\/revisions\/355"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/media\/283"}],"wp:attachment":[{"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=309"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftopic&post=309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}