{"id":3741,"date":"2025-05-28T23:40:17","date_gmt":"2025-05-28T15:40:17","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3741"},"modified":"2025-05-28T23:40:17","modified_gmt":"2025-05-28T15:40:17","slug":"mysql-%e4%b8%ad%e7%9a%84%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b%ef%bc%88stored-procedure%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/05\/28\/mysql-%e4%b8%ad%e7%9a%84%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b%ef%bc%88stored-procedure%ef%bc%89\/","title":{"rendered":"MySQL \u4e2d\u7684\u5b58\u50a8\u8fc7\u7a0b\uff08Stored Procedure\uff09"},"content":{"rendered":"\n<p>MySQL \u4e2d\u7684**\u5b58\u50a8\u8fc7\u7a0b\uff08Stored Procedure\uff09**\u662f\u6307\u4e8b\u5148\u7ecf\u8fc7\u7f16\u8bd1\u5e76\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u7ec4 SQL \u8bed\u53e5\u96c6\u5408\uff0c\u5b83\u53ef\u4ee5\u50cf\u51fd\u6570\u4e00\u6837\u88ab\u53cd\u590d\u8c03\u7528\u3002\u5b83\u4e3b\u8981\u7528\u4e8e\u5c01\u88c5\u4e1a\u52a1\u903b\u8f91\uff0c\u63d0\u9ad8\u53ef\u7ef4\u62a4\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001MySQL \u5b58\u50a8\u8fc7\u7a0b\u7684\u8bed\u6cd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u521b\u5efa\u5b58\u50a8\u8fc7\u7a0b\u7684\u57fa\u672c\u8bed\u6cd5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>DELIMITER $$\n\nCREATE PROCEDURE proc_name (\n    IN param1 INT,         -- \u8f93\u5165\u53c2\u6570\n    OUT param2 VARCHAR(50) -- \u8f93\u51fa\u53c2\u6570\n)\nBEGIN\n    -- SQL \u8bed\u53e5\u5757\n    SELECT column_name INTO param2 FROM table_name WHERE id = param1;\nEND $$\n\nDELIMITER ;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u5b58\u50a8\u8fc7\u7a0b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>DELIMITER $$\n\nCREATE PROCEDURE GetUserNameById (\n    IN userId INT,\n    OUT userName VARCHAR(100)\n)\nBEGIN\n    SELECT name INTO userName FROM users WHERE id = userId;\nEND $$\n\nDELIMITER ;<\/code><\/pre>\n\n\n\n<p>\u8c03\u7528\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CALL GetUserNameById(1, @name);\nSELECT @name;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u5b58\u50a8\u8fc7\u7a0b\u53c2\u6570\u7c7b\u578b<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u53c2\u6570\u7c7b\u578b<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>IN<\/td><td>\u4f20\u5165\u53c2\u6570\uff0c\u8c03\u7528\u65f6\u63d0\u4f9b\u503c<\/td><\/tr><tr><td>OUT<\/td><td>\u8f93\u51fa\u53c2\u6570\uff0c\u8fc7\u7a0b\u5185\u90e8\u8d4b\u503c\u8fd4\u56de<\/td><\/tr><tr><td>INOUT<\/td><td>\u53ef\u4f20\u5165\u4e5f\u53ef\u8fd4\u56de<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u7ba1\u7406\u5b58\u50a8\u8fc7\u7a0b<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u67e5\u770b\u6240\u6709\u5b58\u50a8\u8fc7\u7a0b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW PROCEDURE STATUS WHERE Db = 'your_database_name';<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u67e5\u770b\u5b58\u50a8\u8fc7\u7a0b\u5b9a\u4e49\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW CREATE PROCEDURE procedure_name;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5220\u9664\u5b58\u50a8\u8fc7\u7a0b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>DROP PROCEDURE IF EXISTS procedure_name;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u4f7f\u7528\u573a\u666f\u4e0e\u4f18\u52bf<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u4f18\u52bf\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5c01\u88c5\u4e1a\u52a1\u903b\u8f91\uff1a<\/strong> \u5c01\u88c5\u590d\u6742\u903b\u8f91\uff0c\u4f7f\u5ba2\u6237\u7aef\u4ee3\u7801\u7b80\u6d01\u3002<\/li>\n\n\n\n<li><strong>\u63d0\u9ad8\u6027\u80fd\uff1a<\/strong> \u907f\u514d\u9891\u7e41\u4e0e\u6570\u636e\u5e93\u7684\u4ea4\u4e92\uff0c\u6267\u884c\u901f\u5ea6\u66f4\u5feb\u3002<\/li>\n\n\n\n<li><strong>\u63d0\u9ad8\u5b89\u5168\u6027\uff1a<\/strong> \u63a7\u5236\u5bf9\u6570\u636e\u7684\u8bbf\u95ee\u6743\u9650\uff0c\u907f\u514d\u76f4\u63a5\u66b4\u9732\u8868\u7ed3\u6784\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u91cd\u7528\u6027\u9ad8\uff1a<\/strong> \u5199\u4e00\u6b21\u53ef\u4ee5\u591a\u6b21\u8c03\u7528\uff0c\u7ef4\u62a4\u66f4\u65b9\u4fbf\u3002<\/li>\n\n\n\n<li><strong>\u51cf\u5c11\u7f51\u7edc\u6d41\u91cf\uff1a<\/strong> \u591a\u6761 SQL \u53ef\u4ee5\u5728\u670d\u52a1\u5668\u7aef\u6267\u884c\uff0c\u65e0\u9700\u9891\u7e41\u4f20\u8f93\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccc \u4f7f\u7528\u573a\u666f\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c01\u88c5\u5e38\u7528\u67e5\u8be2\u903b\u8f91\uff08\u5982\u5206\u9875\u67e5\u8be2\u3001\u62a5\u8868\u751f\u6210\uff09<\/li>\n\n\n\n<li>\u4e8b\u52a1\u5904\u7406\uff08\u5728\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u4f7f\u7528 BEGIN \u2026 COMMIT\/ROLLBACK\uff09<\/li>\n\n\n\n<li>\u81ea\u52a8\u5316\u4efb\u52a1\uff08\u7ed3\u5408 Event Scheduler\uff09<\/li>\n\n\n\n<li>\u590d\u6742\u7684\u6279\u5904\u7406\u903b\u8f91<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u62d3\u5c55\u5185\u5bb9\uff08\u81ea\u5b66\uff09<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6761\u4ef6\u63a7\u5236\uff1a<code>IF ... THEN ... ELSE<\/code><\/li>\n\n\n\n<li>\u5faa\u73af\u7ed3\u6784\uff1a<code>WHILE<\/code>\u3001<code>LOOP<\/code>\u3001<code>REPEAT<\/code><\/li>\n\n\n\n<li>\u5f02\u5e38\u5904\u7406\uff1a<code>DECLARE ... HANDLER<\/code><\/li>\n\n\n\n<li>\u6e38\u6807\uff1a<code>DECLARE cursor_name CURSOR FOR SELECT ...<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u5b66\u4e60\u8d44\u6e90\u63a8\u8350<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b98\u65b9\u6587\u6863\uff08MySQL 8.0\uff09\uff1a<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Stored Procedures and Functions:<br>\ud83d\udc49 <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/stored-programs-defining.html\">https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/stored-programs-defining.html<\/a><\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u83dc\u9e1f\u6559\u7a0b\uff08\u4e2d\u6587\uff09\uff1a<\/strong><br>\ud83d\udc49 <a href=\"https:\/\/www.runoob.com\/mysql\/mysql-stored-procedure.html\">https:\/\/www.runoob.com\/mysql\/mysql-stored-procedure.html<\/a><\/li>\n\n\n\n<li><strong>MySQL \u4e8b\u52a1\u5904\u7406 + \u5b58\u50a8\u8fc7\u7a0b\u7efc\u5408\u5b9e\u8df5\uff08\u6781\u5ba2\u65f6\u95f4\u4e13\u680f\/\u535a\u5ba2\uff09<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u63a8\u8350\u4f7f\u7528\u5173\u952e\u5b57 <code>MySQL \u5b58\u50a8\u8fc7\u7a0b \u5b9e\u6218 site:juejin.cn<\/code> \u8fdb\u884c\u641c\u7d22\u9605\u8bfb\u5b9e\u6218\u5185\u5bb9\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL \u4e2d\u7684**\u5b58\u50a8\u8fc7\u7a0b\uff08Stored Procedure\uff09**\u662f\u6307\u4e8b\u5148\u7ecf\u8fc7\u7f16\u8bd1\u5e76\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u7ec4 S [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3742,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-3741","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3741"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3741\/revisions"}],"predecessor-version":[{"id":3743,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3741\/revisions\/3743"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3742"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}