{"id":6223,"date":"2024-11-14T13:10:29","date_gmt":"2024-11-14T05:10:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6223\/"},"modified":"2024-11-14T13:10:29","modified_gmt":"2024-11-14T05:10:29","slug":"mysql%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b%e5%a6%82%e4%bd%95%e5%86%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6223\/","title":{"rendered":"mysql\u5b58\u50a8\u8fc7\u7a0b\u5982\u4f55\u5199"},"content":{"rendered":"<blockquote><p>\n  mysql \u5b58\u50a8\u8fc7\u7a0b\u662f\u4e00\u79cd\u9884\u7f16\u8bd1 sql \u8bed\u53e5\uff0c\u7528\u4e8e\u6267\u884c\u591a\u4e2a\u64cd\u4f5c\uff1a\u521b\u5efa\u8fc7\u7a0b\uff1a\u4f7f\u7528 create procedure \u8bed\u53e5\u521b\u5efa\u5b58\u50a8\u8fc7\u7a0b\u3002\u6307\u5b9a\u53c2\u6570\uff1a\u4f7f\u7528\u8f93\u5165\u548c\u8f93\u51fa\u53c2\u6570\u4f20\u9012\u6570\u636e\u3002\u58f0\u660e\u53d8\u91cf\uff1a\u4f7f\u7528 declare \u8bed\u53e5\u58f0\u660e\u5c40\u90e8\u53d8\u91cf\u3002\u7f16\u5199\u8bed\u53e5\u5757\uff1a\u5305\u542b\u8981\u6267\u884c\u7684 sql \u8bed\u53e5\u3002\u5904\u7406\u9519\u8bef\uff1a\u4f7f\u7528 declare \u548c signal \u8bed\u53e5\u58f0\u660e\u9519\u8bef\u5904\u7406\u53d8\u91cf\u548c\u5f15\u53d1\u9519\u8bef\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/15\/2024061514362746522.jpg\" class=\"aligncenter\" title=\"mysql\u5b58\u50a8\u8fc7\u7a0b\u5982\u4f55\u5199\u63d2\u56fe\" alt=\"mysql\u5b58\u50a8\u8fc7\u7a0b\u5982\u4f55\u5199\u63d2\u56fe\" \/><\/p>\n<h2>MySQL \u5b58\u50a8\u8fc7\u7a0b\u7684\u7f16\u5199<\/h2>\n<p>\u5b58\u50a8\u8fc7\u7a0b\u662f\u4e00\u79cd\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u7684\u9884\u7f16\u8bd1 SQL \u8bed\u53e5\u3002\u5b83\u5141\u8bb8\u7528\u6237\u4f7f\u7528\u5355\u4e2a\u8bed\u53e5\u6267\u884c\u591a\u4e2a\u64cd\u4f5c\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u5e76\u7b80\u5316\u4ee3\u7801\u3002<\/p>\n<h3>\u7f16\u5199 MySQL \u5b58\u50a8\u8fc7\u7a0b\u7684\u6b65\u9aa4\uff1a<\/h3>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u8fc7\u7a0b\uff1a<\/strong>\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\u521b\u5efa\u5b58\u50a8\u8fc7\u7a0b\uff1a<\/p>\n<pre>CREATE PROCEDURE [\u5b58\u50a8\u8fc7\u7a0b\u540d\u79f0] ([\u53c2\u6570\u5217\u8868])\n[\u58f0\u660e\u90e8\u5206]\nBEGIN\n[\u8bed\u53e5\u5757]\nEND<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li> <strong>\u6307\u5b9a\u53c2\u6570\uff1a<\/strong>\u5b58\u50a8\u8fc7\u7a0b\u53ef\u4ee5\u4f7f\u7528\u8f93\u5165\u53c2\u6570\u548c\u8f93\u51fa\u53c2\u6570\u3002\u8f93\u5165\u53c2\u6570\u7528\u4e8e\u5411\u5b58\u50a8\u8fc7\u7a0b\u4f20\u9012\u6570\u636e\uff0c\u800c\u8f93\u51fa\u53c2\u6570\u7528\u4e8e\u5c06\u6570\u636e\u4ece\u5b58\u50a8\u8fc7\u7a0b\u8fd4\u56de\u7ed9\u5916\u90e8\u7a0b\u5e8f\u3002<\/li>\n<li> <strong>\u58f0\u660e\u53d8\u91cf\uff1a<\/strong>\u5728\u5b58\u50a8\u8fc7\u7a0b\u7684\u4e3b\u4f53\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 DECLARE \u8bed\u53e5\u58f0\u660e\u5c40\u90e8\u53d8\u91cf\u3002<\/li>\n<li> <strong>\u7f16\u5199\u8bed\u53e5\u5757\uff1a<\/strong>\u5b58\u50a8\u8fc7\u7a0b\u7684\u4e3b\u4f53\u5305\u542b\u8981\u6267\u884c\u7684 SQL \u8bed\u53e5\u3002<\/li>\n<li> <strong>\u5904\u7406\u9519\u8bef\uff1a<\/strong>\u4f7f\u7528 DECLARE \u8bed\u53e5\u58f0\u660e\u9519\u8bef\u5904\u7406\u53d8\u91cf\uff0c\u5e76\u4f7f\u7528 SIGNAL \u8bed\u53e5\u5f15\u53d1\u9519\u8bef\u3002<\/li>\n<\/ol>\n<h3>\u793a\u4f8b\uff1a<\/h3>\n<p>\u4e0b\u9762\u7684\u793a\u4f8b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a get_customer \u7684\u5b58\u50a8\u8fc7\u7a0b\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u5ba2\u6237 ID \u4f5c\u4e3a\u8f93\u5165\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5ba2\u6237\u4fe1\u606f\u4f5c\u4e3a\u8f93\u51fa\u53c2\u6570\uff1a<\/p>\n<pre>CREATE PROCEDURE get_customer(IN customer_id INT)\nBEGIN\n  DECLARE customer_name VARCHAR(255);\n  DECLARE customer_email VARCHAR(255);\n\n  SELECT name, email\n  INTO customer_name, customer_email\n  FROM customers\n  WHERE customer_id = customer_id;\n\n  SET @name = customer_name;\n  SET @email = customer_email;\nEND<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3>\u4f7f\u7528\u5b58\u50a8\u8fc7\u7a0b\uff1a<\/h3>\n<p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u4f7f\u7528\u5b58\u50a8\u8fc7\u7a0b\uff1a<\/p>\n<pre>CALL [\u5b58\u50a8\u8fc7\u7a0b\u540d\u79f0]([\u53c2\u6570\u503c\u5217\u8868])<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u4f8b\u4e2d\uff0c\u53ef\u4ee5\u5982\u4e0b\u65b9\u5f0f\u8c03\u7528 get_customer \u5b58\u50a8\u8fc7\u7a0b\uff1a<\/p>\n<pre>CALL get_customer(123)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5b58\u50a8\u8fc7\u7a0b\u5982\u4f55\u5199\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mysql \u5b58\u50a8\u8fc7\u7a0b\u662f\u4e00\u79cd\u9884\u7f16\u8bd1 sql \u8bed\u53e5\uff0c\u7528\u4e8e\u6267\u884c\u591a\u4e2a\u64cd\u4f5c\uff1a\u521b\u5efa\u8fc7\u7a0b\uff1a\u4f7f\u7528 create procedure \u8bed\u53e5\u521b\u5efa\u5b58\u50a8\u8fc7\u7a0b\u3002\u6307\u5b9a\u53c2\u6570\uff1a\u4f7f\u7528\u8f93\u5165\u548c\u8f93\u51fa\u53c2\u6570\u4f20\u9012\u6570\u636e\u3002\u58f0\u660e\u53d8\u91cf\uff1a\u4f7f\u7528 declare \u8bed\u53e5\u58f0\u660e\u5c40\u90e8\u53d8\u91cf\u3002\u7f16\u5199\u8bed\u53e5\u5757\uff1a\u5305\u542b\u8981\u6267\u884c\u7684 sql \u8bed\u53e5\u3002\u5904\u7406\u9519\u8bef\uff1a\u4f7f\u7528 declare \u548c signal \u8bed\u53e5\u58f0\u660e\u9519\u8bef\u5904\u7406\u53d8\u91cf\u548c\u5f15\u53d1\u9519\u8bef\u3002 MySQL \u5b58\u50a8\u8fc7\u7a0b\u7684\u7f16\u5199 \u5b58\u50a8\u8fc7\u7a0b\u662f\u4e00\u79cd\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u7684\u9884\u7f16\u8bd1 SQL \u8bed\u53e5\u3002\u5b83\u5141\u8bb8\u7528\u6237\u4f7f\u7528\u5355\u4e2a\u8bed\u53e5\u6267\u884c\u591a\u4e2a\u64cd\u4f5c\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u5e76\u7b80\u5316\u4ee3\u7801\u3002 \u7f16\u5199 MySQL \u5b58\u50a8\u8fc7\u7a0b\u7684\u6b65\u9aa4\uff1a \u521b\u5efa\u8fc7\u7a0b\uff1a\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\u521b\u5efa\u5b58\u50a8\u8fc7\u7a0b\uff1a CREATE PROCEDURE [\u5b58\u50a8\u8fc7\u7a0b\u540d\u79f0] ([\u53c2\u6570\u5217\u8868]) [\u58f0\u660e\u90e8\u5206] BEGIN [\u8bed\u53e5\u5757] END \u767b\u5f55\u540e\u590d\u5236 \u6307\u5b9a\u53c2\u6570\uff1a\u5b58\u50a8\u8fc7\u7a0b\u53ef\u4ee5\u4f7f\u7528\u8f93\u5165\u53c2\u6570\u548c\u8f93\u51fa\u53c2\u6570\u3002\u8f93\u5165\u53c2\u6570\u7528\u4e8e\u5411\u5b58\u50a8\u8fc7\u7a0b\u4f20\u9012\u6570\u636e\uff0c\u800c\u8f93\u51fa\u53c2\u6570\u7528\u4e8e\u5c06\u6570\u636e\u4ece\u5b58\u50a8\u8fc7\u7a0b\u8fd4\u56de\u7ed9\u5916\u90e8\u7a0b\u5e8f\u3002 \u58f0\u660e\u53d8\u91cf\uff1a\u5728\u5b58\u50a8\u8fc7\u7a0b\u7684\u4e3b\u4f53\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 DECLARE \u8bed\u53e5\u58f0\u660e\u5c40\u90e8\u53d8\u91cf\u3002 \u7f16\u5199\u8bed\u53e5\u5757\uff1a\u5b58\u50a8\u8fc7\u7a0b\u7684\u4e3b\u4f53\u5305\u542b\u8981\u6267\u884c\u7684 SQL \u8bed\u53e5\u3002 \u5904\u7406\u9519\u8bef\uff1a\u4f7f\u7528 DECLARE \u8bed\u53e5\u58f0\u660e\u9519\u8bef\u5904\u7406\u53d8\u91cf\uff0c\u5e76\u4f7f\u7528 SIGNAL \u8bed\u53e5\u5f15\u53d1\u9519\u8bef\u3002 \u793a\u4f8b\uff1a \u4e0b\u9762\u7684\u793a\u4f8b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a get_customer \u7684\u5b58\u50a8\u8fc7\u7a0b\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u5ba2\u6237 ID \u4f5c\u4e3a\u8f93\u5165\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5ba2\u6237\u4fe1\u606f\u4f5c\u4e3a\u8f93\u51fa\u53c2\u6570\uff1a CREATE PROCEDURE get_customer(IN customer_id [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-6223","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=6223"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6223\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}