{"id":168413,"date":"2024-11-06T12:10:28","date_gmt":"2024-11-06T04:10:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/168413\/"},"modified":"2024-11-06T12:10:28","modified_gmt":"2024-11-06T04:10:28","slug":"sql-%e5%be%aa%e7%92%b0%e6%8f%92%e5%85%a5%e7%9a%84%e8%aa%9e%e5%8f%a5%e5%af%ab%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/168413\/","title":{"rendered":"SQL \u5faa\u74b0\u63d2\u5165\u7684\u8a9e\u53e5\u5beb\u6cd5"},"content":{"rendered":"<h1 id=\"sql-%e5%be%aa%e7%92%b0%e6%8f%92%e5%85%a5%e7%9a%84%e8%aa%9e%e5%8f%a5%e5%af%ab%e6%b3%95-VsGMNHrnPi\">SQL \u5faa\u74b0\u63d2\u5165\u7684\u8a9e\u53e5\u5beb\u6cd5<\/h1>\n<p>\u5728\u8cc7\u6599\u5eab\u7ba1\u7406\u4e2d\uff0cSQL\uff08\u7d50\u69cb\u5316\u67e5\u8a62\u8a9e\u8a00\uff09\u662f\u4e00\u7a2e\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\uff0c\u5c24\u5176\u662f\u5728\u9032\u884c\u6578\u64da\u63d2\u5165\u6642\u3002\u7576\u9700\u8981\u63d2\u5165\u5927\u91cf\u6578\u64da\u6642\uff0c\u4f7f\u7528\u5faa\u74b0\u63d2\u5165\u7684\u8a9e\u53e5\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u6548\u7387\u3002\u672c\u6587\u5c07\u63a2\u8a0e SQL \u5faa\u74b0\u63d2\u5165\u7684\u8a9e\u53e5\u5beb\u6cd5\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u7bc4\u4f8b\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e5%be%aa%e7%92%b0%e6%8f%92%e5%85%a5%ef%bc%9f-VsGMNHrnPi\">\u4ec0\u9ebc\u662f\u5faa\u74b0\u63d2\u5165\uff1f<\/h2>\n<p>\u5faa\u74b0\u63d2\u5165\u662f\u6307\u5728\u4e00\u500b\u5faa\u74b0\u7d50\u69cb\u4e2d\u91cd\u8907\u57f7\u884c\u63d2\u5165\u64cd\u4f5c\u3002\u9019\u7a2e\u65b9\u6cd5\u7279\u5225\u9069\u5408\u65bc\u9700\u8981\u5f9e\u5176\u4ed6\u6578\u64da\u6e90\uff08\u5982\u6587\u4ef6\u6216\u5176\u4ed6\u8868\uff09\u6279\u91cf\u5c0e\u5165\u6578\u64da\u7684\u60c5\u6cc1\u3002\u4f7f\u7528\u5faa\u74b0\u63d2\u5165\u53ef\u4ee5\u6e1b\u5c11\u591a\u6b21\u57f7\u884c\u63d2\u5165\u8a9e\u53e5\u7684\u958b\u92b7\uff0c\u5f9e\u800c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e5%9f%ba%e6%9c%ac%e8%aa%9e%e6%b3%95-VsGMNHrnPi\">\u57fa\u672c\u8a9e\u6cd5<\/h2>\n<p>\u5728 SQL \u4e2d\uff0c\u5faa\u74b0\u63d2\u5165\u901a\u5e38\u8207\u5b58\u5132\u904e\u7a0b\u6216\u8173\u672c\u7d50\u5408\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 PL\/SQL\uff08Oracle \u7684\u4e00\u7a2e SQL \u64f4\u5c55\uff09\u9032\u884c\u5faa\u74b0\u63d2\u5165\u7684\u57fa\u672c\u8a9e\u6cd5\uff1a<\/p>\n<pre><code>\nDECLARE\n    CURSOR c1 IS SELECT * FROM source_table;\n    v_record source_table%ROWTYPE;\nBEGIN\n    OPEN c1;\n    LOOP\n        FETCH c1 INTO v_record;\n        EXIT WHEN c1%NOTFOUND;\n        INSERT INTO target_table (column1, column2, column3)\n        VALUES (v_record.column1, v_record.column2, v_record.column3);\n    END LOOP;\n    CLOSE c1;\nEND;\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5b9a\u7fa9\u4e86\u4e00\u500b\u6e38\u6a19\u4f86\u9078\u64c7\u4f86\u6e90\u8868\u4e2d\u7684\u6240\u6709\u8a18\u9304\u3002\u7136\u5f8c\uff0c\u6211\u5011\u4f7f\u7528\u4e00\u500b\u5faa\u74b0\u4f86\u9010\u884c\u63d0\u53d6\u8a18\u9304\u4e26\u63d2\u5165\u5230\u76ee\u6a19\u8868\u4e2d\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8-mysql-%e7%9a%84%e5%be%aa%e7%92%b0%e6%8f%92%e5%85%a5-VsGMNHrnPi\">\u4f7f\u7528 MySQL \u7684\u5faa\u74b0\u63d2\u5165<\/h2>\n<p>\u5728 MySQL \u4e2d\uff0c\u96d6\u7136\u4e0d\u652f\u6301 PL\/SQL\uff0c\u4f46\u6211\u5011\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u5b58\u5132\u904e\u7a0b\u4f86\u5be6\u73fe\u985e\u4f3c\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u500b MySQL \u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>\nDELIMITER \/\/\nCREATE PROCEDURE InsertData()\nBEGIN\n    DECLARE done INT DEFAULT FALSE;\n    DECLARE v_column1 INT;\n    DECLARE v_column2 VARCHAR(255);\n    DECLARE cur CURSOR FOR SELECT column1, column2 FROM source_table;\n    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;\n\n    OPEN cur;\n\n    read_loop: LOOP\n        FETCH cur INTO v_column1, v_column2;\n        IF done THEN\n            LEAVE read_loop;\n        END IF;\n        INSERT INTO target_table (column1, column2) VALUES (v_column1, v_column2);\n    END LOOP;\n\n    CLOSE cur;\nEND \/\/\nDELIMITER ;\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u540d\u70ba <code>InsertData<\/code> \u7684\u5b58\u5132\u904e\u7a0b\uff0c\u4f7f\u7528\u6e38\u6a19\u4f86\u904d\u6b77\u4f86\u6e90\u8868\u7684\u6578\u64da\uff0c\u4e26\u5c07\u5176\u63d2\u5165\u5230\u76ee\u6a19\u8868\u4e2d\u3002<\/p>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-VsGMNHrnPi\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<ul>\n<li><strong>\u6027\u80fd\u8003\u91cf\uff1a<\/strong>\u5728\u9032\u884c\u5927\u91cf\u6578\u64da\u63d2\u5165\u6642\uff0c\u5efa\u8b70\u4f7f\u7528\u6279\u91cf\u63d2\u5165\u7684\u65b9\u5f0f\uff0c\u9019\u6a23\u53ef\u4ee5\u6e1b\u5c11\u4e8b\u52d9\u7684\u958b\u92b7\u3002<\/li>\n<li><strong>\u932f\u8aa4\u8655\u7406\uff1a<\/strong>\u5728\u5faa\u74b0\u63d2\u5165\u4e2d\uff0c\u61c9\u8a72\u8003\u616e\u5230\u932f\u8aa4\u8655\u7406\u6a5f\u5236\uff0c\u4ee5\u907f\u514d\u56e0\u70ba\u55ae\u4e00\u8a18\u9304\u7684\u932f\u8aa4\u800c\u5c0e\u81f4\u6574\u500b\u63d2\u5165\u904e\u7a0b\u5931\u6557\u3002<\/li>\n<li><strong>\u4e8b\u52d9\u7ba1\u7406\uff1a<\/strong>\u5728\u9032\u884c\u63d2\u5165\u64cd\u4f5c\u6642\uff0c\u61c9\u8a72\u6839\u64da\u9700\u8981\u4f7f\u7528\u4e8b\u52d9\u4f86\u78ba\u4fdd\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-VsGMNHrnPi\">\u7e3d\u7d50<\/h2>\n<p>\u5faa\u74b0\u63d2\u5165\u662f\u4e00\u7a2e\u6709\u6548\u7684\u6578\u64da\u63d2\u5165\u65b9\u6cd5\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u6279\u91cf\u8655\u7406\u6578\u64da\u6642\u3002\u901a\u904e\u4f7f\u7528\u6e38\u6a19\u548c\u5b58\u5132\u904e\u7a0b\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07\u6578\u64da\u5f9e\u4e00\u500b\u8868\u63d2\u5165\u5230\u53e6\u4e00\u500b\u8868\u4e2d\u3002\u7121\u8ad6\u662f\u4f7f\u7528 Oracle \u7684 PL\/SQL \u9084\u662f MySQL \u7684\u5b58\u5132\u904e\u7a0b\uff0c\u638c\u63e1\u5faa\u74b0\u63d2\u5165\u7684\u6280\u5de7\u90fd\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u5404\u7a2e\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3 SQL \u5faa\u74b0\u63d2\u5165\u7684\u8a9e\u53e5\u5beb\u6cd5\uff0c\u638c\u63e1\u5982\u4f55\u4f7f\u7528\u8ff4\u5708\u6709\u6548\u5730\u5c07\u591a\u7b46\u8cc7\u6599\u63d2\u5165\u8cc7\u6599\u5eab\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-168413","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168413","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=168413"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168413\/revisions"}],"predecessor-version":[{"id":168414,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168413\/revisions\/168414"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=168413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=168413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=168413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}