{"id":144479,"date":"2019-05-27T09:40:43","date_gmt":"2019-05-27T01:40:43","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=144479"},"modified":"2019-05-21T09:41:58","modified_gmt":"2019-05-21T01:41:58","slug":"mariadb-process-control","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mariadb-process-control.html","title":{"rendered":"Mariadb\u5b66\u4e60\u603b\u7ed3\uff08\u5341\u4e8c\uff09\uff1a\u590d\u5408\u8bed\u53e5\u548c\u6d41\u7a0b\u63a7\u5236"},"content":{"rendered":"
\u5728MariaDB 10.1.1+\u7248\u672c\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5b58\u50a8\u8fc7\u7a0b\u4ee5\u5916\u6765\u4f7f\u7528\u590d\u5408\u8bed\u53e5\u4e86\uff0c\u987e\u540d\u601d\u4e49\uff0c\u590d\u5408\u8bed\u53e5\u5c31\u662f\u5c06\u591a\u6761\u8bed\u53e5\u4f5c\u4e3a\u4e00\u4e2a\u6574\u4f53\u6765\u6267\u884c\uff0c\u53ef\u4ee5\u5728\u5176\u4e2d\u4f7f\u7528\u4e00\u4e9b\u903b\u8f91\u5224\u65ad\uff0c\u5faa\u73af\u7b49\u529f\u80fd\uff0c\u5927\u5927\u63d0\u9ad8\u4e86SQL\u8bed\u8a00\u7684\u53ef\u7f16\u7a0b\u6027\u3002<\/p>\n
\u5728\u5b58\u50a8\u8fc7\u7a0b\u4ee5\u5916\u4f7f\u7528\u590d\u5408\u8bed\u53e5\u9700\u8981\u9075\u5b88\u4ee5\u4e0b\u7ea6\u5b9a\uff1a<\/p>\n
\u5f53\u8981\u4f7f\u7528\u590d\u5408\u8bed\u53e5\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u7684\u683c\u5f0f\u6765\u4f7f\u7528\uff1a<\/p>\n
BEGIN [NOT ATOMIC]\r\n [statement_list]\r\nEND\r\n<\/pre>\n\u56e0\u4e3aSQL\u8bed\u53e5\u8981\u4f7f\u7528;\u6765\u4f5c\u4e3a\u7ed3\u675f\u7684\u6807\u8bc6\u7b26\uff0c\u4f46\u662f\uff0c\u591a\u6761SQL\u8bed\u53e5\uff0c\u5230\u5e95\u54ea\u4e2a\u624d\u662f\u7ed3\u675f\u7b26\u4e86\uff1f\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u5c06\u590d\u5408\u8bed\u53e5\u7684\u7ed3\u675f\u7b26\u4fee\u6539\u4e3a\u5176\u4ed6\u5b57\u7b26\uff0c\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\u5373\u53ef\uff1a|\u53ef\u4ee5\u66ff\u6362\u4e3a\u4efb\u610f\u6570\u91cf\u7684\u4efb\u610f\u5b57\u7b26\u3002<\/p>\n
delimiter |\r\n\/\/\u8be5\u547d\u4ee4\u5728\u5f53\u524d\u4f1a\u8bdd\u6709\u6548\uff0c\u4f1a\u5f71\u54cd\u540e\u7eed\u6240\u6709SQL\u8bed\u53e5\u7684\u7ed3\u675f\u7b26\r\n<\/pre>\n\u590d\u5408\u8bed\u53e5\u4f8b\u5b50<\/span><\/strong><\/div>\nMariaDB [world]> DELIMITER ||\r\nMariaDB [world]> BEGIN NOT ATOMIC\r\n -> SELECT * FROM user;\r\n -> SELECT * FROM department;\r\n -> END\r\n -> ||\r\n<\/pre>\n\u4f1a\u987a\u5e8f\u663e\u793a\u4e24\u5f20\u8868\u7684\u5185\u5bb9\uff0c\u4f46\u662f\uff0c\u6709\u4ec0\u4e48\u5462\uff1f<\/p>\n
\u563f\uff0c\u90fd\u8bf4\u4e86\uff0c\u589e\u52a0\u4e86\u53ef\u7f16\u7a0b\u6027\uff0c\u8fd8\u6ca1\u5224\u65ad\u3001\u5faa\u73af\u5462\u3002<\/p>\n
\u5b9a\u4e49\u672c\u5730\u53d8\u91cf<\/span><\/strong><\/div>\n
\n\u672c\u5730\u53d8\u91cf\u4ec5\u5728\u5f53\u524dBEGIN..END\u5185\u751f\u6548<\/strong>\uff0c\u5b9a\u4e49\u4e00\u4e2a\u672c\u5730\u53d8\u91cf\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\nDECLARE var_name [, var_name] type [DEFAULT value]\r\n\r\ntype\u5c31\u662fMariaDB\u4e2d\u652f\u6301\u7684\u90a3\u4e9b\u6570\u636e\u7c7b\u578b\u3002\r\n<\/pre>\n\u6bd4\u5982\u5982\u4e0b\u4f8b\u5b50\uff1a\u67e5\u8be2test1\u7528\u6237\u7684\u7ec4ID\u5e76\u653e\u5165\u5230tmpdid\u53d8\u91cf\u4e2d\u53bb<\/p>\n
MariaDB [world]> BEGIN NOT ATOMIC\r\n -> DECLARE tmpdid INT DEFAULT 0;\r\n -> SELECT deptid INTO tmpdid FROM user WHERE name='test1';\r\n -> SELECT tmpdid;\r\n -> END|\r\n+--------+\r\n| tmpdid |\r\n+--------+\r\n| 1 |\r\n+--------+\r\n1 row in set (0.00 sec)\r\n<\/pre>\n\u6240\u4ee5\u8bf4\uff0cBEGIN...END\u662f\u53ef\u4ee5\u5d4c\u5957\u4f7f\u7528\u7684\uff0c\u5982\u5728IF\u8bed\u53e5\u4e2d\u4f7f\u7528BEGIN...END\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b9a\u4e49\u57df\uff0c\u5f53\u7136BEGIN...END\u4e5f\u662f\u5f00\u542f\u4e8b\u52a1\u7684\u6807\u5fd7<\/p>\n
IF\u8bed\u53e5<\/span><\/strong><\/div>\n\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n
IF search_condition THEN statement_list\r\n [ELSEIF search_condition THEN statement_list] ...\r\n [ELSE statement_list]\r\nEND IF\r\n<\/pre>\n\u8fd9\u4e2a\u5c31\u8ddf\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u4e00\u6837\u4e86\uff0c\u4e0d\u518d\u7d6e\u53e8\uff0c\u6765\u8bd5\u8bd5\u5427\u3002<\/p>\n
BEGIN NOT ATOMIC\r\n DECLARE tmpdid INT DEFAULT 0;\r\n SELECT deptid INTO tmpdid FROM user WHERE name='test1';\r\n IF (tmpdid = 1) THEN \r\n SELECT 'The User test1 is the member of salse';\r\n ELSE\r\n SELECT * FROM department WHERE id = tmpdid;\r\n END IF;\r\n END|\r\n<\/pre>\n\u6240\u4ee5search_condition\u53ea\u8981\u662f\u53ef\u4ee5\u8868\u8fbeTRUE\u6216FALSE\u7684\u8868\u8fbe\u5f0f\u90fd\u884c\uff0c\u6bd4\u5982\uff1a<\/p>\n
BEGIN NOT ATOMIC\r\n IF EXISTS(SELECT * FROM user WHERE name = 'lucy') THEN\r\n SELECT 'Find the user lucy';\r\n ELSE\r\n SELECT 'Not Find';\r\n END IF;\r\n END|\r\n+--------------------+\r\n| Find the user lucy |\r\n+--------------------+\r\n| Find the user lucy |\r\n+--------------------+\r\n1 row in set (0.00 sec)\r\n\r\n<\/pre>\nCASE \u8bed\u53e5<\/span><\/strong><\/div>\nCASE\u6709\u4e24\u79cd\u7528\u6cd5\uff0c\u4e00\u79cd\u662f\u50cf\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684SWITCH\u4e00\u6837\uff0c\u8fdb\u884c\u6570\u636e\u7684\u6311\u9009\uff0c\u53e6\u4e00\u79cd\u5219\u662f\u5b9e\u73b0\u4e86\u591a\u5206\u652f\u7684IF-ELSE\u8bed\u53e5\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n
\/\/\u5bf9\u67d0\u4e00\u4e2a\u503c\u8fdb\u884c\u7b5b\u9009\r\nCASE case_value\r\n WHEN when_value THEN statement_list\r\n [WHEN when_value THEN statement_list] ...\r\n [ELSE statement_list]\r\nEND CASE\r\n\r\n\/\/\u591a\u5206\u652f\u7684IF-ELSE\r\nCASE\r\n WHEN search_condition THEN statement_list\r\n [WHEN search_condition THEN statement_list] ...\r\n [ELSE statement_list] \r\nEND CASE\r\n<\/pre>\n...\u60f3\u4e0d\u51fa\u6765\u4f8b\u5b50\uff0c\u653e\u5f03\uff0c\u54ea\u5929\u60f3\u5230\u4e86\u518d\u6765\u8865\u5427\u3002<\/p>\n
\u9644\u5b98\u65b9\u4f8b\u5b50\u4e00\u679a\uff1a<\/p>\n
DELIMITER |\r\nCREATE PROCEDURE p()\r\nBEGIN\r\n DECLARE v INT DEFAULT 1;\r\n CASE v\r\n WHEN 2 THEN SELECT v;\r\n WHEN 3 THEN SELECT 0;\r\n ELSE BEGIN END;\r\n END CASE;\r\nEND;\r\n|<\/pre>\nLOOP\u8bed\u53e5<\/span><\/strong><\/div>\nLOOP\u4e4b\u524d\u6ca1\u6709\u7528\u8fc7\uff0c\u597d\u50cf\u5f88\u591a\u7f16\u7a0b\u8bed\u8a00\u91cc\u90fd\u6ca1\u6709LOOP\u8fd9\u4e2a\u5173\u952e\u5b57\u4e86\uff0c\u7528\u6765\u5b9e\u73b0\u7b80\u5355\u7684\u5faa\u73af\uff0c\u9700\u8981\u914d\u5408LEAVE\u8bed\u53e5\u8df3\u51fa\u5faa\u73af\u3002<\/p>\n
\u8bbe\u7f6e\u4e00\u53d8\u91cf\uff0c\u5c06\u5176\u52a0\u5230100\u5e76\u9000\u51fa\uff1a\u4e3a\u5565\u8981\u52a0\u5230100\uff1f\u9b3c\u77e5\u9053\uff0c\u54c8\u54c8\u54c8\u3002<\/p>\n
BEGIN NOT ATOMIC\r\n DECLARE tempNum INT DEFAULT 0;\r\n test:LOOP\r\n SET tempNum=tempNum+1;\r\n IF tempNum=100 THEN\r\n LEAVE test;\r\n END IF;\r\n END LOOP test;\r\n SELECT tempNum;\r\nEND;\r\n |\r\n<\/pre>\n\u6240\u4ee5LOOP\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n
[begin_label:] LOOP\r\n statement_list\r\nEND LOOP [end_label]\r\n<\/pre>\n\u901a\u5e38\uff0c\u9700\u8981\u8bbe\u7f6e\u4e00\u4e2abegin_label\uff0c\u7528\u4e8e\u6807\u8bc6\u8be5\u5faa\u73af\uff0c\u4ee5\u65b9\u4fbf\u4f7f\u7528LEAVE\u8df3\u51fa\u8be5\u5faa\u73af\uff0c\u800cend_label\u53ef\u4ee5\u7701\u7565\uff0c\u4f46\u662f\u5982\u679c\u60f3\u8981\u7ed9end_label\u7684\u8bdd\uff0c\u5fc5\u987b\u4e0ebegin_label\u7684\u540d\u79f0\u76f8\u540c\uff0c\u800cLEAVE\u7684\u8bed\u6cd5\u5c31\u5f88\u7b80\u5355\u4e86\uff1a<\/p>\n
LEAVE label\r\n<\/pre>\nWHILE\u8bed\u53e5<\/span><\/strong><\/div>\nWHILE\u5c31\u4e0e\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u4e00\u6837\u4e86\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n
[begin_label:] WHILE search_condition DO\r\n statement_list\r\nEND WHILE [end_label]\r\n<\/pre>\n\u5f53search_condition\u8868\u8fbe\u5f0f\u4e0d\u4e3aTRUE\u65f6\u5219\u4e0d\u518d\u6267\u884c\u5faa\u73af\u3002<\/p>\n
\u67e5\u627e\u67d0\u4e00\u7528\u6237\u7684ID\u503c\u4e3a\u591a\u5c11\uff0c\u4e3a\u4ec0\u4e48\u8981\u5199\u4e2a\u5faa\u73af\u5462\uff1f\u4e0d\u77e5\u9053\u5440\uff0c\u7528WHERE\u4e0d\u66f4\u597d\u5417\uff1f<\/p>\n
REPEAT..LOOP\u5faa\u73af<\/span><\/strong><\/div>\nREPEAT\u5faa\u73af\u770b\u8d77\u6765\u975e\u5e38\u50cfdo...while\u5faa\u73af\uff0c\u597d\u5427\uff0c\u5176\u5b9e\u5c31\u662f\u4e00\u56de\u4e8b\u3002<\/p>\n
\u7b2c\u4e00\u6b21\u5faa\u73af\u4f53\u4e0d\u5224\u65ad\u4efb\u4f55\u6761\u4ef6\u6267\u884c\u4e00\u6b21\uff0c\u7136\u540e\u518d\u5224\u65ad\u6761\u4ef6\uff0c\u5982\u679c\u6761\u4ef6\u8fd8\u6ee1\u8db3\u5219\u7ee7\u7eed\u6267\u884c\uff0c\u76f4\u5230\u6761\u4ef6\u4e0d\u6ee1\u8db3\u4e3a\u4e4b\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n
[begin_label:] REPEAT\r\n statement_list\r\nUNTIL search_condition\r\nEND REPEAT [end_label]\r\n<\/pre>\n\u62fc\u63a5\u6240\u6709\u7528\u6237\u540d\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff1a<\/p>\n
MariaDB [world]> BEGIN NOT ATOMIC\r\n -> DECLARE i INT DEFAULT 1;\r\n -> DECLARE userNames VARCHAR(200) DEFAULT '';\r\n -> DECLARE tmpName VARCHAR(10) DEFAULT '';\r\n -> DECLARE userNums INT DEFAULT 0;\r\n -> SELECT COUNT(id) INTO userNums FROM user;\r\n -> REPEAT \r\n -> SELECT name INTO tmpName FROM user WHERE id = i;\r\n -> SET userNames = CONCAT(userNames,',',tmpName);\r\n -> SET i = i +1;\r\n -> UNTIL NOT i< =userNums -> END REPEAT;\r\n -> SELECT userNames;\r\n -> END|\r\n+--------------------------------------------------------------------------------------------+\r\n| userNames |\r\n+--------------------------------------------------------------------------------------------+\r\n| ,test,test1,lucy,mars,mark,test6,test7,test7,test8,test8,test9,test10,test11,test12,test13 |\r\n+--------------------------------------------------------------------------------------------+\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"\u590d\u5408\u8bed\u53e5 \u5728MariaDB 10.1.1+\u7248\u672c\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5b58\u50a8\u8fc7\u7a0b\u4ee5\u5916\u6765\u4f7f\u7528\u590d\u5408\u8bed\u53e5\u4e86\uff0c\u987e\u540d\u601d\u4e49\uff0c\u590d\u5408\u8bed\u53e5\u5c31 […]<\/p>\n","protected":false},"author":317,"featured_media":100961,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-144479","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/144479","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/317"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=144479"}],"version-history":[{"count":15,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/144479\/revisions"}],"predecessor-version":[{"id":145491,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/144479\/revisions\/145491"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/100961"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=144479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=144479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=144479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}