{"id":200082,"date":"2025-05-06T08:23:17","date_gmt":"2025-05-06T00:23:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200082\/"},"modified":"2025-05-06T08:23:17","modified_gmt":"2025-05-06T00:23:17","slug":"%e6%b7%b1%e5%85%a5%e8%a7%a3%e6%9e%90mysql%e4%b8%ad%e7%9a%84proc%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200082\/","title":{"rendered":"\u6df1\u5165\u89e3\u6790MySQL\u4e2d\u7684proc\u8868"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u6df1\u5165\u89e3\u6790MySQL\u4e2d\u7684proc\u8868<\/h1>\n<p>MySQL.proc\u8868\u662fMySQL\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u4e2a\u7cfb\u7edf\u8868\uff0c\u5b58\u50a8\u4e86\u6240\u6709\u5b58\u50a8\u8fc7\u7a0b\u7684\u4fe1\u606f\u3002\u901a\u8fc7\u67e5\u8be2\u8be5\u8868\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u83b7\u53d6\u5b58\u50a8\u8fc7\u7a0b\u7684\u540d\u79f0\u3001\u7c7b\u578b\u3001\u53c2\u6570\u3001\u4e3b\u4f53\u7b49\u8be6\u7ec6\u4fe1\u606f\uff0c\u65b9\u4fbf\u8fdb\u884c\u5b58\u50a8\u8fc7\u7a0b\u7684\u7ba1\u7406\u548c\u7ef4\u62a4\u3002\u8be5\u8868\u5728MySQL\u4e2d\u8d77\u7740\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\uff0c\u662f\u5b58\u50a8\u8fc7\u7a0b\u5f00\u53d1\u548c\u7ba1\u7406\u7684\u5173\u952e\u3002<\/p>\n<p>MySQL.proc\u8868\u7684\u4f5c\u7528\u548c\u529f\u80fd\u8be6\u89e3<\/p>\n<p>MySQL\u662f\u4e00\u79cd\u6d41\u884c\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff0c\u5f00\u53d1\u8005\u5728\u4f7f\u7528MySQL\u65f6\u5e38\u5e38\u4f1a\u6d89\u53ca\u5230\u5b58\u50a8\u8fc7\u7a0b\uff08Stored Procedure\uff09\u7684\u521b\u5efa\u548c\u7ba1\u7406\u3002\u800cMySQL.proc\u8868\u5219\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u7cfb\u7edf\u8868\uff0c\u5b83\u5b58\u50a8\u4e86\u6570\u636e\u5e93\u4e2d\u6240\u6709\u7684\u5b58\u50a8\u8fc7\u7a0b\u7684\u76f8\u5173\u4fe1\u606f\uff0c\u5305\u62ec\u5b58\u50a8\u8fc7\u7a0b\u7684\u540d\u79f0\u3001\u5b9a\u4e49\u3001\u53c2\u6570\u7b49\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u89e3\u91caMySQL.proc\u8868\u7684\u4f5c\u7528\u548c\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>MySQL.proc\u8868\u662f\u4e00\u4e2a\u7cfb\u7edf\u8868\uff0c\u53ef\u4ee5\u901a\u8fc7\u67e5\u8be2\u8be5\u8868\u6765\u83b7\u53d6\u6570\u636e\u5e93\u4e2d\u6240\u6709\u5b58\u50a8\u8fc7\u7a0b\u7684\u76f8\u5173\u4fe1\u606f\u3002\u4e0b\u9762\u662fMySQL.proc\u8868\u7684\u7ed3\u6784\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>Field<\/th>\n<th>Type<\/th>\n<th>Null<\/th>\n<th>Key<\/th>\n<th>Default<\/th>\n<th>Extra<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>db<\/td>\n<td>char(64)<\/td>\n<td>NO<\/td>\n<td>PRI<\/td>\n<td> <\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>name<\/td>\n<td>char(64)<\/td>\n<td>NO<\/td>\n<td>PRI<\/td>\n<td> <\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>type<\/td>\n<td>enum(&#8216;PROCEDURE&#8217;,&#8217;FUNCTION&#8217;)<\/td>\n<td>NO<\/td>\n<td>PRI<\/td>\n<td> <\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>specific_name<\/td>\n<td>char(64)<\/td>\n<td>NO<\/td>\n<td> <\/td>\n<td> <\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>language<\/td>\n<td>enum(&#8216;SQL&#8217;)<\/td>\n<td>NO<\/td>\n<td> <\/td>\n<td>SQL<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>sql_data_access<\/td>\n<td>enum(&#8216;CONTAINS SQL&#8217;,&#8217;NO SQL&#8217;,&#8217;READS SQL DATA&#8217;,&#8217;MODIFIES SQL DATA&#8217;)<\/td>\n<td>NO<\/td>\n<td> <\/td>\n<td> <\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>is_deterministic<\/td>\n<td>enum(&#8216;NO&#8217;,&#8217;YES&#8217;)<\/td>\n<td>NO<\/td>\n<td> <\/td>\n<td>NO<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>security_type<\/td>\n<td>enum(&#8216;DEFINER&#8217;,&#8217;INVOKER&#8217;)<\/td>\n<td>NO<\/td>\n<td> <\/td>\n<td>DEFINER<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>param_list<\/td>\n<td>blob<\/td>\n<td>YES<\/td>\n<td> <\/td>\n<td>NULL<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>returns<\/td>\n<td>longblob<\/td>\n<td>YES<\/td>\n<td> <\/td>\n<td>NULL<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>body<\/td>\n<td>longblob<\/td>\n<td>YES<\/td>\n<td> <\/td>\n<td>NULL<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>definer<\/td>\n<td>char(77)<\/td>\n<td>NO<\/td>\n<td> <\/td>\n<td> <\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>created<\/td>\n<td>timestamp<\/td>\n<td>NO<\/td>\n<td> <\/td>\n<td>CURRENT_TIMESTAMP<\/td>\n<td>on update CURRENT_TIMESTAMP<\/td>\n<\/tr>\n<tr>\n<td>modified<\/td>\n<td>timestamp<\/td>\n<td>NO<\/td>\n<td> <\/td>\n<td>0000-00-00 00:00:00<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>sql_mode<\/td>\n<td>set(&#8216;REAL_AS_FLOAT&#8217;,&#8217;PIPES_AS_CONCAT&#8217;,&#8217;ANSI_QUOTES&#8217;,&#8217;IGNORE_SPACE&#8217;,&#8217;NOT_USED&#8217;,&#8217;ONLY_FULL_GROUP_BY&#8217;,&#8217;NO_UNSIGNED_SUBTRACTION&#8217;,&#8217;NO_DIR_IN_CREATE&#8217;,&#8217;POSTGRESQL&#8217;,&#8217;ORACLE&#8217;,&#8217;MSSQL&#8217;,&#8217;DB2&#8242;,&#8217;MAXDB&#8217;,&#8217;NO_KEY_OPTIONS&#8217;,&#8217;NO_TABLE_OPTIONS&#8217;,&#8217;NO_FIELD_OPTIONS&#8217;,&#8217;MYSQL323&#8242;,&#8217;MYSQL40&#8242;,&#8217;ANSI&#8217;,&#8217;NO_AUTO_VALUE_ON_ZERO&#8217;,&#8217;NO_BACKSLASH_ESCAPES&#8217;,&#8217;STRICT_TRANS_TABLES&#8217;,&#8217;STRICT_ALL_TABLES&#8217;,&#8217;NO_ZERO_IN_DATE&#8217;,&#8217;NO_ZERO_DATE&#8217;,&#8217;INVALID_DATES&#8217;,&#8217;ERROR_FOR_DIVISION_BY_ZERO&#8217;,&#8217;HIGH_NOT_PRECEDENCE&#8217;,&#8217;NO_AUTO_CREATE_USER&#8217;,&#8217;NO_ENGINE_SUBSTITUTION&#8217;,&#8217;PAD_CHAR_TO_FULL_LENGTH&#8217;)<\/td>\n<td>YES<\/td>\n<td> <\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>comment<\/td>\n<td>text<\/td>\n<td>YES<\/td>\n<td> <\/td>\n<td>NULL<\/td>\n<td> <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e0a\u8868\u5217\u51fa\u4e86MySQL.proc\u8868\u7684\u5404\u4e2a\u5b57\u6bb5\uff0c\u8fd9\u4e9b\u5b57\u6bb5\u8bb0\u5f55\u4e86\u5b58\u50a8\u8fc7\u7a0b\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u4f8b\u5982\u5b58\u50a8\u8fc7\u7a0b\u6240\u5c5e\u7684\u6570\u636e\u5e93(db)\u3001\u5b58\u50a8\u8fc7\u7a0b\u7684\u540d\u79f0(name)\u3001\u5b58\u50a8\u8fc7\u7a0b\u7c7b\u578b(type)\u3001\u53c2\u6570\u5217\u8868(param_list)\u3001\u5b58\u50a8\u8fc7\u7a0b\u7684\u4e3b\u4f53(body)\u7b49\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u6765\u6f14\u793a\u5982\u4f55\u67e5\u8be2MySQL.proc\u8868\u4e2d\u7684\u4fe1\u606f\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a\u201cget_customer_info\u201d\u7684\u5b58\u50a8\u8fc7\u7a0b\uff0c\u5176\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre>DELIMITER $$\nCREATE PROCEDURE get_customer_info (IN customer_id INT)\nBEGIN\n    SELECT * FROM customers WHERE id = customer_id;\nEND $$\nDELIMITER ;<\/pre>\n<p>\u5b58\u50a8\u8fc7\u7a0b\u201cget_customer_info\u201d\u7528\u4e8e\u6839\u636e\u5ba2\u6237ID\u67e5\u8be2\u5ba2\u6237\u4fe1\u606f\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0bSQL\u8bed\u53e5\u67e5\u8be2MySQL.proc\u8868\uff0c\u67e5\u770b\u5b58\u50a8\u8fc7\u7a0b\u201cget_customer_info\u201d\u7684\u4fe1\u606f\uff1a<\/p>\n<pre>SELECT * FROM mysql.proc WHERE db = 'my_database' AND name = 'get_customer_info';<\/pre>\n<p>\u5728\u8fd9\u4e2a\u67e5\u8be2\u7ed3\u679c\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5b58\u50a8\u8fc7\u7a0b\u201cget_customer_info\u201d\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5305\u62ec\u53c2\u6570\u5217\u8868\u3001\u5b58\u50a8\u8fc7\u7a0b\u8bed\u53e5\u7b49\u3002<\/p>\n<p>MySQL.proc\u8868\u662fMySQL\u4e2d\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u7cfb\u7edf\u8868\uff0c\u5b83\u662f\u5b58\u50a8\u8fc7\u7a0b\u7ba1\u7406\u7684\u5173\u952e\u3002\u901a\u8fc7\u67e5\u8be2MySQL.proc\u8868\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u83b7\u53d6\u6570\u636e\u5e93\u4e2d\u6240\u6709\u5b58\u50a8\u8fc7\u7a0b\u7684\u4fe1\u606f\uff0c\u65b9\u4fbf\u8fdb\u884c\u5b58\u50a8\u8fc7\u7a0b\u7684\u7ba1\u7406\u548c\u7ef4\u62a4\u3002<\/p>\n<p>\u603b\u7684\u6765\u8bf4\uff0cMySQL.proc\u8868\u7684\u4e3b\u8981\u4f5c\u7528\u548c\u529f\u80fd\u5305\u62ec\uff1a<\/p>\n<ol>\n<li>\u5b58\u50a8\u6570\u636e\u5e93\u4e2d\u6240\u6709\u5b58\u50a8\u8fc7\u7a0b\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/li>\n<li>\u63d0\u4f9b\u4e86\u67e5\u8be2\u548c\u7ba1\u7406\u5b58\u50a8\u8fc7\u7a0b\u7684\u4fbf\u5229\u6027\u3002<\/li>\n<li>\u8bb0\u5f55\u4e86\u5b58\u50a8\u8fc7\u7a0b\u7684\u5b9a\u4e49\u3001\u53c2\u6570\u7b49\u91cd\u8981\u4fe1\u606f\u3002<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u8bfb\u8005\u5bf9MySQL.proc\u8868\u7684\u4f5c\u7528\u548c\u529f\u80fd\u6709\u4e86\u66f4\u6df1\u5165\u7684\u4e86\u89e3\u3002\u5e0c\u671b\u672c\u6587\u7684\u5185\u5bb9\u80fd\u591f\u5bf9\u4f7f\u7528MySQL\u8fdb\u884c\u5b58\u50a8\u8fc7\u7a0b\u5f00\u53d1\u7684\u5f00\u53d1\u8005\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u6df1\u5165\u89e3\u6790MySQL\u4e2d\u7684proc\u8868\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6570\u636e\u5e93\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u89e3\u6790MySQL\u4e2d\u7684proc\u8868&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200082","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200082","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200082"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200082\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}