{"id":5963,"date":"2024-11-14T08:45:34","date_gmt":"2024-11-14T00:45:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5963\/"},"modified":"2024-11-14T08:45:34","modified_gmt":"2024-11-14T00:45:34","slug":"sql%e5%be%aa%e7%8e%af%e8%af%ad%e5%8f%a5%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5963\/","title":{"rendered":"sql\u5faa\u73af\u8bed\u53e5\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  sql\u4e2d\u7684\u5faa\u73af\u8bed\u53e5\uff08while\u5faa\u73af\uff09\u5141\u8bb8\u5f00\u53d1\u8005\u91cd\u590d\u6267\u884c\u64cd\u4f5c\uff0c\u7528\u4e8e\u904d\u5386\u6570\u636e\u96c6\u5e76\u6267\u884c\u64cd\u4f5c\uff0c\u7c7b\u4f3c\u4e8e\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684for\u5faa\u73af\u3002\u5176\u7528\u6cd5\u6b65\u9aa4\u4e3a\uff1a\u521b\u5efa\u6e38\u6807\uff0c\u6253\u5f00\u6e38\u6807\uff0c\u4f7f\u7528\u5faa\u73af\u8bed\u53e5\u904d\u5386\uff0c\u6267\u884c\u64cd\u4f5c\uff0c\u6700\u540e\u5173\u95ed\u6e38\u6807\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/12\/2024061202452143360.jpg\" class=\"aligncenter\" title=\"sql\u5faa\u73af\u8bed\u53e5\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"sql\u5faa\u73af\u8bed\u53e5\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u5faa\u73af\u8bed\u53e5\u7b80\u4ecb<\/strong><\/p>\n<p>\u5728 SQL \u4e2d\uff0c\u5faa\u73af\u8bed\u53e5\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u91cd\u590d\u6267\u884c\u4e00\u7ec4\u64cd\u4f5c\u3002\u5b83\u7528\u4e8e\u904d\u5386\u6570\u636e\u96c6\u5e76\u5e94\u7528\u67d0\u4e9b\u64cd\u4f5c\uff0c\u7c7b\u4f3c\u4e8e\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684 for \u5faa\u73af\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>WHILE condition DO\n    statement1;\n    statement2;\n    ...\nEND WHILE;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u53c2\u6570<\/strong><\/p>\n<ul>\n<li> <strong>condition\uff1a<\/strong>\u5224\u65ad\u662f\u5426\u7ee7\u7eed\u6267\u884c\u5faa\u73af\u7684\u6761\u4ef6\u3002<\/li>\n<li> <strong>statement1, statement2, &#8230;\uff1a<\/strong>\u8981\u5728\u6bcf\u4e2a\u8fed\u4ee3\u4e2d\u6267\u884c\u7684 SQL \u8bed\u53e5\u3002<\/li>\n<\/ul>\n<p><strong>\u7528\u6cd5<\/strong><\/p>\n<ol>\n<li> <strong>\u521b\u5efa\u6e38\u6807\uff1a<\/strong>\u9996\u5148\uff0c\u60a8\u9700\u8981\u4f7f\u7528 DECLARE \u8bed\u53e5\u521b\u5efa\u6e38\u6807\uff0c\u5b83\u5c06\u5b58\u50a8\u60a8\u8981\u904d\u5386\u7684\u6570\u636e\u96c6\u3002<\/li>\n<li> <strong>\u6253\u5f00\u6e38\u6807\uff1a<\/strong>\u4f7f\u7528 OPEN \u8bed\u53e5\u6253\u5f00\u6e38\u6807\u3002<\/li>\n<li> <strong>\u4f7f\u7528\u5faa\u73af\u8bed\u53e5\uff1a<\/strong>\u4f7f\u7528 WHILE \u5faa\u73af\u8bed\u53e5\u6765\u904d\u5386\u6e38\u6807\u4e2d\u7684\u6bcf\u4e00\u884c\u3002<\/li>\n<li> <strong>\u6267\u884c\u64cd\u4f5c\uff1a<\/strong>\u5728\u5faa\u73af\u4f53\u5185\uff0c\u6267\u884c\u6240\u9700\u7684 SQL \u8bed\u53e5\u6765\u64cd\u4f5c\u6bcf\u4e00\u884c\u6570\u636e\u3002<\/li>\n<li> <strong>\u5173\u95ed\u6e38\u6807\uff1a<\/strong>\u6700\u540e\uff0c\u4f7f\u7528 CLOSE \u8bed\u53e5\u5173\u95ed\u6e38\u6807\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u540d\u4e3a &#8220;customers&#8221; \u7684\u8868\uff0c\u5176\u4e2d\u5305\u542b\u5ba2\u6237\u4fe1\u606f\u3002\u4ee5\u4e0b SQL \u8bed\u53e5\u5c06\u4f7f\u7528\u5faa\u73af\u6765\u66f4\u65b0\u5ba2\u6237\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff1a<\/p>\n<pre>-- \u521b\u5efa\u6e38\u6807\nDECLARE customer_cursor CURSOR FOR SELECT customer_id, email FROM customers;\n\n-- \u6253\u5f00\u6e38\u6807\nOPEN customer_cursor;\n\n-- \u4f7f\u7528\u5faa\u73af\u66f4\u65b0\u7535\u5b50\u90ae\u4ef6\u5730\u5740\nWHILE TRUE DO\n    FETCH customer_cursor INTO customer_id, email;\n    IF customer_id IS NULL THEN\n        EXIT;\n    END IF;\n\n    -- \u66f4\u65b0\u7535\u5b50\u90ae\u4ef6\u5730\u5740\n    UPDATE customers SET email = 'new_email@example.com' WHERE customer_id = customer_id;\nEND WHILE;\n\n-- \u5173\u95ed\u6e38\u6807\nCLOSE customer_cursor;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u91cd\u8981\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u5faa\u73af\u8bed\u53e5\u53ef\u80fd\u4f1a\u9020\u6210\u6b7b\u5faa\u73af\uff0c\u56e0\u6b64\u786e\u4fdd\u60a8\u7684\u6761\u4ef6\u6700\u7ec8\u4f1a\u5bfc\u81f4\u5faa\u73af\u7ed3\u675f\u3002<\/li>\n<li>\u5faa\u73af\u53ef\u4ee5\u5d4c\u5957\u5728\u5176\u4ed6\u5faa\u73af\u5185\u3002<\/li>\n<li>\u5faa\u73af\u8bed\u53e5\u901a\u5e38\u7528\u4e8e\u5904\u7406\u5927\u91cf\u6570\u636e\uff0c\u4f46\u5b83\u4eec\u53ef\u80fd\u4f1a\u589e\u52a0\u6570\u636e\u5e93\u7684\u5904\u7406\u65f6\u95f4\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u5faa\u73af\u8bed\u53e5\u600e\u4e48\u7528\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>sql\u4e2d\u7684\u5faa\u73af\u8bed\u53e5\uff08while\u5faa\u73af\uff09\u5141\u8bb8\u5f00\u53d1\u8005\u91cd\u590d\u6267\u884c\u64cd\u4f5c\uff0c\u7528\u4e8e\u904d\u5386\u6570\u636e\u96c6\u5e76\u6267\u884c\u64cd\u4f5c\uff0c\u7c7b\u4f3c\u4e8e\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684for\u5faa\u73af\u3002\u5176\u7528\u6cd5\u6b65\u9aa4\u4e3a\uff1a\u521b\u5efa\u6e38\u6807\uff0c\u6253\u5f00\u6e38\u6807\uff0c\u4f7f\u7528\u5faa\u73af\u8bed\u53e5\u904d\u5386\uff0c\u6267\u884c\u64cd\u4f5c\uff0c\u6700\u540e\u5173\u95ed\u6e38\u6807\u3002 SQL \u5faa\u73af\u8bed\u53e5\u7b80\u4ecb \u5728 SQL \u4e2d\uff0c\u5faa\u73af\u8bed\u53e5\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u91cd\u590d\u6267\u884c\u4e00\u7ec4\u64cd\u4f5c\u3002\u5b83\u7528\u4e8e\u904d\u5386\u6570\u636e\u96c6\u5e76\u5e94\u7528\u67d0\u4e9b\u64cd\u4f5c\uff0c\u7c7b\u4f3c\u4e8e\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684 for \u5faa\u73af\u3002 \u8bed\u6cd5 WHILE condition DO statement1; statement2; &#8230; END WHILE; \u767b\u5f55\u540e\u590d\u5236 \u53c2\u6570 condition\uff1a\u5224\u65ad\u662f\u5426\u7ee7\u7eed\u6267\u884c\u5faa\u73af\u7684\u6761\u4ef6\u3002 statement1, statement2, &#8230;\uff1a\u8981\u5728\u6bcf\u4e2a\u8fed\u4ee3\u4e2d\u6267\u884c\u7684 SQL \u8bed\u53e5\u3002 \u7528\u6cd5 \u521b\u5efa\u6e38\u6807\uff1a\u9996\u5148\uff0c\u60a8\u9700\u8981\u4f7f\u7528 DECLARE \u8bed\u53e5\u521b\u5efa\u6e38\u6807\uff0c\u5b83\u5c06\u5b58\u50a8\u60a8\u8981\u904d\u5386\u7684\u6570\u636e\u96c6\u3002 \u6253\u5f00\u6e38\u6807\uff1a\u4f7f\u7528 OPEN \u8bed\u53e5\u6253\u5f00\u6e38\u6807\u3002 \u4f7f\u7528\u5faa\u73af\u8bed\u53e5\uff1a\u4f7f\u7528 WHILE \u5faa\u73af\u8bed\u53e5\u6765\u904d\u5386\u6e38\u6807\u4e2d\u7684\u6bcf\u4e00\u884c\u3002 \u6267\u884c\u64cd\u4f5c\uff1a\u5728\u5faa\u73af\u4f53\u5185\uff0c\u6267\u884c\u6240\u9700\u7684 SQL \u8bed\u53e5\u6765\u64cd\u4f5c\u6bcf\u4e00\u884c\u6570\u636e\u3002 \u5173\u95ed\u6e38\u6807\uff1a\u6700\u540e\uff0c\u4f7f\u7528 CLOSE \u8bed\u53e5\u5173\u95ed\u6e38\u6807\u3002 \u793a\u4f8b \u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u540d\u4e3a &#8220;customers&#8221; \u7684\u8868\uff0c\u5176\u4e2d\u5305\u542b\u5ba2\u6237\u4fe1\u606f\u3002\u4ee5\u4e0b SQL \u8bed\u53e5\u5c06\u4f7f\u7528\u5faa\u73af\u6765\u66f4\u65b0\u5ba2\u6237\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff1a &#8212; \u521b\u5efa\u6e38\u6807 DECLARE customer_cursor CURSOR FOR SELECT 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-5963","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5963","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=5963"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5963\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}