{"id":309821,"date":"2024-01-20T19:12:22","date_gmt":"2024-01-20T11:12:22","guid":{"rendered":"https:\/\/www.idc.net\/help\/309821\/"},"modified":"2024-01-20T19:12:22","modified_gmt":"2024-01-20T11:12:22","slug":"mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/309821\/","title":{"rendered":"MySQL\u6570\u636e\u5e93\u7684\u5f02\u5e38\u5904\u7406"},"content":{"rendered":"<p><strong>MySQL\u6570\u636e\u5e93<\/strong>\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u96be\u514d\u4f1a\u51fa\u73b0\u4f1a\u51fa\u73b0\u4e00\u4e9b\u5f02\u5e38\u60c5\u51b5\uff0c\u9047\u5230\u8fd9\u79cd\u60c5\u51b5\u662f\uff0c\u4e0b\u6587\u4e2d\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c06\u4f1a\u5e2e\u52a9\u5230\u5927\u5bb6\u3002<\/p>\n<p><strong>\u5bf9\u4e8eMySQL\u7684\u5f02\u5e38\u5904\u7406\uff1a<\/strong><\/p>\n<p>\u6807\u51c6\u683c\u5f0f<\/p>\n<p>\u4ee5\u4e0b\u4e3a\u5f15\u7528\u7684\u5185\u5bb9\uff1a<\/p>\n<p>\u3000DECLARE handler_type HANDLER FOR condition_value[,...] statement<br \/> \u3000\u3000handler_type:<br \/> \u3000\u3000CONTINUE<br \/> \u3000\u3000| EXIT<br \/> \u3000\u3000| UNDO --\u6682\u65f6\u4e0d\u652f\u6301<br \/> \u3000\u3000condition_value:<br \/> \u3000\u3000SQLSTATE [VALUE] sqlstate_value<br \/> \u3000\u3000| condition_name<br \/> \u3000\u3000| SQLWARNING<br \/> \u3000\u3000| NOT FOUND<br \/> \u3000\u3000| SQLEXCEPTION<br \/> \u3000\u3000| mysql_error_code<\/p>\n<p>\u3000\u3000condition_value\u7ec6\u8282 <br \/> &nbsp;<\/p>\n<p><strong>1\u3001MySQL ERROR CODE \u5217\u8868<\/strong><\/p>\n<p>\u5982\u679c\u9700\u8981\u67e5\u770b\u66f4\u591a\u7684\u9519\u8bef\u5217\u8868\u53ef\u4ee5\u76f4\u63a5\u5230MySQL\u5b89\u88c5\u8def\u5f84\u4e0b\u3002<\/p>\n<p>\u6bd4\u5982\u6211\u7684\/usr\/local\/mysql\/share\/mysql\/errmsg.txt<\/p>\n<p>\u8bf4\u660e\uff1aSQLSTATE [VALUE] sqlstate_value\u8fd9\u79cd\u683c\u5f0f\u662f\u4e13\u95e8\u4e3aANSI SQL \u548c ODBC\u4ee5\u53ca\u5176\u4ed6\u7684\u6807\u51c6.<\/p>\n<p>\u5e76\u4e0d\u662f\u6240\u6709\u7684MySQL ERROR CODE \u90fd\u6620\u5c04\u5230SQLSTATE\u3002<\/p>\n<p><strong>2\u3001\u5047\u5982\u4e0d\u9700\u8981\u63d2\u5165ERROR CODE\uff0c\u53ef\u4ee5\u7528\u901f\u8bb0\u6761\u4ef6\u6765\u4ee3\u66ff <\/strong><\/p>\n<p>SQLWARNING \u4ee3\u8868\u6240\u6709\u4ee501\u5f00\u5934\u7684\u9519\u8bef\u4ee3\u7801<\/p>\n<p>NOT FOUND \u4ee3\u8868\u6240\u6709\u4ee502\u5f00\u5934\u7684\u9519\u8bef\u4ee3\u7801\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u4ee3\u8868\u4e00\u4e2a\u6e38\u6807\u5230\u8fbe\u6570\u636e\u96c6\u7684\u672b\u5c3e\u3002<\/p>\n<p>SQLEXCEPTION \u4ee3\u8868\u9664\u4e86SQLWARNING\u548cNOT FOUND \u7684\u6240\u6709\u9519\u8bef\u4ee3\u7801\u3002<\/p>\n<p><strong>3\u3001\u5177\u4f53\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4e3a\u5f15\u7528\u7684\u5185\u5bb9\uff1a<\/p>\n<p>\u3000\u3000create TABLE t (s1 int,primary key (s1));<br \/> \u3000\u3000mysql&gt; use t_girl<br \/> \u3000\u3000Database changed<br \/> \u3000\u3000mysql&gt; create TABLE t (s1 int,primary key (s1));<br \/> \u3000\u3000Query OK, 0 rows affected (0.00 sec)<br \/> \u3000\u3000mysql&gt;<br \/> \u3000\u3000mysql&gt;<br \/> \u3000\u3000mysql&gt; DELIMITER ||<br \/> \u3000\u3000mysql&gt; create PROCEDURE handlerdemo ()<br \/> \u3000\u3000-&gt; BEGIN<br \/> \u3000\u3000-&gt; DECLARE EXIT HANDLER FOR SQLSTATE \u201923000\u2019 BEGIN END; -- \u9047\u5230\u91cd\u590d\u952e\u503c\u5c31\u9000\u51fa<br \/> \u3000\u3000-&gt; SET @x = 1;<br \/> \u3000\u3000-&gt; insert INTO t VALUES (1);<br \/> \u3000\u3000-&gt; SET @x = 2;<br \/> \u3000\u3000-&gt; insert INTO t VALUES (1);<br \/> \u3000\u3000-&gt; SET @x = 3;<br \/> \u3000\u3000-&gt; END||<br \/> \u3000\u3000Query OK, 0 rows affected (0.00 sec)<br \/> \u3000\u3000mysql&gt; DELIMITER ;<br \/> \u3000\u3000mysql&gt; call handlerdemo();<br \/> \u3000\u3000Query OK, 0 rows affected (0.00 sec)<br \/> \u3000\u3000mysql&gt; select @x;<br \/> \u3000\u3000+------+<br \/> \u3000\u3000| @x |<br \/> \u3000\u3000+------+<br \/> \u3000\u3000| 2 |<br \/> \u3000\u3000+------+<br \/> \u3000\u30001 row in set (0.00 sec)<br \/> \u3000\u3000mysql&gt; call handlerdemo();<br \/> \u3000\u3000Query OK, 0 rows affected (0.00 sec)<br \/> \u3000\u3000mysql&gt; select @x;<br \/> \u3000\u3000+------+<br \/> \u3000\u3000| @x |<br \/> \u3000\u3000+------+<br \/> \u3000\u3000| 1 |<br \/> \u3000\u3000+------+<br \/> \u3000\u30001 row in set (0.00 sec)<br \/> \u3000\u3000mysql&gt;<br \/> &nbsp;<\/p>\n<p>MySQL\u6570\u636e\u5e93\u7684\u5f02\u5e38\u53ef\u80fd\u8fd8\u6709\u5f88\u591a\u79cd\uff0c\u8fd9\u91cc\u5c31\u5148\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\uff0c\u4f9b\u5927\u5bb6\u53c2\u8003\uff0c\u4ee5\u540e\u6211\u8fd8\u4f1a\u7ee7\u7eed\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u8fd9\u65b9\u9762\u7684\u77e5\u8bc6\u3002<\/p>\n<p>\u3010\u7f16\u8f91\u63a8\u8350\u3011<\/p>\n<ol>\n<li>\u5982\u4f55\u5173\u95edMySQL\u6570\u636e\u5e93\u4e2d\u9519\u8bef\u63d0\u793a\u97f3<\/li>\n<li>\u63d0\u9ad8MySQL\u6570\u636e\u5e93\u67e5\u8be2\u6548\u7387\u7684\u6280\u5de7\uff08\u4e00\uff09<\/li>\n<li>\u8ba4\u8bc6MySQL\u6570\u636e\u5e93\u5bf9\u670d\u52a1\u5668\u7aef\u5149\u6807\u7684\u9650\u5236<\/li>\n<li>MySQL\u6570\u636e\u5e93\u53ea\u76d1\u542c\u67d0\u4e2a\u7279\u5b9a\u5730\u5740\u7684\u65b9\u6cd5<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>MySQL\u6570\u636e\u5e93\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u96be\u514d\u4f1a\u51fa\u73b0\u4f1a\u51fa\u73b0\u4e00\u4e9b\u5f02\u5e38\u60c5\u51b5\uff0c\u9047\u5230\u8fd9\u79cd\u60c5\u51b5\u662f\uff0c\u4e0b\u6587\u4e2d\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c06\u4f1a\u5e2e\u52a9\u5230\u5927\u5bb6\u3002 \u5bf9\u4e8e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7038],"tags":[],"class_list":["post-309821","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/309821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=309821"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/309821\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=309821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=309821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=309821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}