{"id":78274,"date":"2024-10-16T11:44:16","date_gmt":"2024-10-16T03:44:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/78274\/"},"modified":"2024-10-16T11:44:16","modified_gmt":"2024-10-16T03:44:16","slug":"mysql%e6%8c%87%e4%bb%a4-declare%ef%bc%88%e8%81%b2%e6%98%8e%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/78274\/","title":{"rendered":"MySQL\u6307\u4ee4: DECLARE\uff08\u8072\u660e\uff09"},"content":{"rendered":"<h1 id=\"mysql%e6%8c%87%e4%bb%a4-declare%ef%bc%88%e8%81%b2%e6%98%8e%ef%bc%89-LKdfgfSpDx\">MySQL\u6307\u4ee4: DECLARE\uff08\u8072\u660e\uff09<\/h1>\n<p>\u5728MySQL\u4e2d\uff0c<code>DECLARE<\/code>\u6307\u4ee4\u662f\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\uff0c\u4e3b\u8981\u7528\u65bc\u5728\u5b58\u5132\u904e\u7a0b\u6216\u89f8\u767c\u5668\u4e2d\u8072\u660e\u8b8a\u91cf\u3001\u6e38\u6a19\u548c\u689d\u4ef6\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u6df1\u5165\u63a2\u8a0e<code>DECLARE<\/code>\u7684\u7528\u6cd5\u53ca\u5176\u5728\u6578\u64da\u5eab\u64cd\u4f5c\u4e2d\u7684\u61c9\u7528\u3002<\/p>\n<h2 id=\"1-declare%e7%9a%84%e5%9f%ba%e6%9c%ac%e8%aa%9e%e6%b3%95-LKdfgfSpDx\">1. DECLARE\u7684\u57fa\u672c\u8a9e\u6cd5<\/h2>\n<p>\u5728MySQL\u4e2d\uff0c<code>DECLARE<\/code>\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>DECLARE variable_name datatype [DEFAULT value];\nDECLARE cursor_name CURSOR FOR select_statement;\nDECLARE condition_name CONDITION FOR condition_value;<\/code><\/pre>\n<p>\u9019\u88e1\uff0c<code>variable_name<\/code>\u662f\u8b8a\u91cf\u7684\u540d\u7a31\uff0c<code>datatype<\/code>\u662f\u6578\u64da\u985e\u578b\uff0c<code>cursor_name<\/code>\u662f\u6e38\u6a19\u7684\u540d\u7a31\uff0c<code>select_statement<\/code>\u662f\u7528\u65bc\u6e38\u6a19\u7684\u67e5\u8a62\u8a9e\u53e5\uff0c<code>condition_name<\/code>\u662f\u689d\u4ef6\u7684\u540d\u7a31\uff0c<code>condition_value<\/code>\u662f\u5177\u9ad4\u7684\u689d\u4ef6\u503c\u3002<\/p>\n<h2 id=\"2-%e8%81%b2%e6%98%8e%e8%ae%8a%e9%87%8f-LKdfgfSpDx\">2. \u8072\u660e\u8b8a\u91cf<\/h2>\n<p>\u5728\u5b58\u5132\u904e\u7a0b\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>DECLARE<\/code>\u4f86\u8072\u660e\u8b8a\u91cf\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\uff1a<\/p>\n<pre><code>DELIMITER \/\/\nCREATE PROCEDURE example_procedure()\nBEGIN\n    DECLARE total_sales DECIMAL(10, 2) DEFAULT 0.00;\n    SELECT SUM(sales) INTO total_sales FROM sales_table;\n    SELECT total_sales;\nEND \/\/\nDELIMITER ;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u8072\u660e\u4e86\u4e00\u500b\u540d\u70ba<code>total_sales<\/code>\u7684\u8b8a\u91cf\uff0c\u4e26\u5c07\u5176\u7528\u65bc\u5b58\u5132\u5f9e<code>sales_table<\/code>\u4e2d\u8a08\u7b97\u51fa\u7684\u7e3d\u92b7\u552e\u984d\u3002<\/p>\n<h2 id=\"3-%e8%81%b2%e6%98%8e%e6%b8%b8%e6%a8%99-LKdfgfSpDx\">3. \u8072\u660e\u6e38\u6a19<\/h2>\n<p>\u6e38\u6a19\u5141\u8a31\u60a8\u9010\u884c\u8655\u7406\u67e5\u8a62\u7d50\u679c\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u8072\u660e\u548c\u4f7f\u7528\u6e38\u6a19\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>DELIMITER \/\/\nCREATE PROCEDURE cursor_example()\nBEGIN\n    DECLARE done INT DEFAULT FALSE;\n    DECLARE customer_name VARCHAR(100);\n    DECLARE customer_cursor CURSOR FOR SELECT name FROM customers;\n    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;\n\n    OPEN customer_cursor;\n\n    read_loop: LOOP\n        FETCH customer_cursor INTO customer_name;\n        IF done THEN\n            LEAVE read_loop;\n        END IF;\n        SELECT customer_name;\n    END LOOP;\n\n    CLOSE customer_cursor;\nEND \/\/\nDELIMITER ;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u8072\u660e\u4e86\u4e00\u500b\u6e38\u6a19<code>customer_cursor<\/code>\uff0c\u7528\u65bc\u5f9e<code>customers<\/code>\u8868\u4e2d\u9078\u64c7\u5ba2\u6236\u540d\u7a31\u3002\u901a\u904e\u4f7f\u7528<code>FETCH<\/code>\u8a9e\u53e5\uff0c\u6211\u5011\u53ef\u4ee5\u9010\u884c\u7372\u53d6\u6578\u64da\uff0c\u4e26\u5728\u5faa\u74b0\u4e2d\u8655\u7406\u6bcf\u4e00\u884c\u3002<\/p>\n<h2 id=\"4-%e8%81%b2%e6%98%8e%e6%a2%9d%e4%bb%b6-LKdfgfSpDx\">4. \u8072\u660e\u689d\u4ef6<\/h2>\n<p>\u5728\u5b58\u5132\u904e\u7a0b\u4e2d\uff0c\u60a8\u9084\u53ef\u4ee5\u8072\u660e\u689d\u4ef6\u4ee5\u8655\u7406\u7279\u5b9a\u60c5\u6cc1\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\uff1a<\/p>\n<pre><code>DELIMITER \/\/\nCREATE PROCEDURE condition_example()\nBEGIN\n    DECLARE exit_condition CONDITION FOR SQLSTATE '45000';\n    DECLARE CONTINUE HANDLER FOR exit_condition BEGIN\n        SELECT 'An error occurred!';\n    END;\n\n    -- \u9019\u88e1\u53ef\u4ee5\u653e\u7f6e\u4e00\u4e9b\u53ef\u80fd\u5f15\u767c\u932f\u8aa4\u7684\u4ee3\u78bc\n    SIGNAL exit_condition;\nEND \/\/\nDELIMITER ;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u8072\u660e\u4e86\u4e00\u500b\u689d\u4ef6<code>exit_condition<\/code>\uff0c\u4e26\u8a2d\u7f6e\u4e86\u4e00\u500b\u8655\u7406\u7a0b\u5e8f\u4f86\u6355\u7372\u8a72\u689d\u4ef6\u3002\u7576\u767c\u751f\u932f\u8aa4\u6642\uff0c\u5c07\u986f\u793a\u76f8\u61c9\u7684\u6d88\u606f\u3002<\/p>\n<h2 id=\"5-%e5%b0%8f%e7%b5%90-LKdfgfSpDx\">5. \u5c0f\u7d50<\/h2>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0c<code>DECLARE<\/code>\u6307\u4ee4\u5728MySQL\u4e2d\u626e\u6f14\u8457\u81f3\u95dc\u91cd\u8981\u7684\u89d2\u8272\uff0c\u7279\u5225\u662f\u5728\u5b58\u5132\u904e\u7a0b\u548c\u89f8\u767c\u5668\u7684\u958b\u767c\u4e2d\u3002\u901a\u904e\u8072\u660e\u8b8a\u91cf\u3001\u6e38\u6a19\u548c\u689d\u4ef6\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u9748\u6d3b\u5730\u8655\u7406\u6578\u64da\u548c\u932f\u8aa4\uff0c\u5f9e\u800c\u63d0\u9ad8\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u6548\u7387\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bcMySQL\u7684\u4f7f\u7528\u6709\u9032\u4e00\u6b65\u7684\u9700\u6c42\uff0c\u6216\u662f\u9700\u8981\u9ad8\u6548\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u6b61\u8fce\u4e86\u89e3\u6211\u5011\u7684<a href=\"https:\/\/server.hk\">VPS<\/a>\u670d\u52d9\uff0c\u63d0\u4f9b\u7a69\u5b9a\u7684\u6578\u64da\u5eab\u74b0\u5883\uff0c\u52a9\u60a8\u8f15\u9b06\u7ba1\u7406\u6578\u64da\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3MySQL\u4e2d\u7684DECLARE\u6307\u4ee4\uff0c\u5b78\u7fd2\u5982\u4f55\u8072\u660e\u8b8a\u6578\u4ee5\u5728\u5b58\u5132\u904e\u7a0b\u548c\u89f8\u767c\u5668\u4e2d\u4f7f\u7528\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6548\u7387\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-78274","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/78274","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=78274"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/78274\/revisions"}],"predecessor-version":[{"id":78275,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/78274\/revisions\/78275"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=78274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=78274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=78274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}