{"id":15952,"date":"2023-12-04T23:46:00","date_gmt":"2023-12-04T15:46:00","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=15952"},"modified":"2023-12-05T00:21:09","modified_gmt":"2023-12-04T16:21:09","slug":"oracle-zhong-xie-fen-ye-you-na-xie-fang-fa-ni-yi-b","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/04\/oracle-zhong-xie-fen-ye-you-na-xie-fang-fa-ni-yi-b\/","title":{"rendered":"Oracle \u4e2d\u5199\u5206\u9875\u6709\u54ea\u4e9b\u65b9\u6cd5\uff0c\u4f60\u4e00\u822c\u7528\u4ec0\u4e48\u65b9\u6cd5\uff1f\u7528 SQL \u8bed\u53e5\u5199\u4e00\u4e2a\u5206\u9875\uff1f\u5982\u4f55\u7528\u5b58\u50a8\u8fc7\u7a0b\u5199\u5206\u9875\uff1f"},"content":{"rendered":"<p>\u5728 Oracle \u4e2d\uff0c\u5b9e\u73b0\u5206\u9875\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2d\u4e00\u4e9b\u5e38\u89c1\u7684\u5305\u62ec\u4f7f\u7528 <code>ROWNUM<\/code>\u3001<code>OFFSET FETCH<\/code> \u5b50\u53e5\u4ee5\u53ca\u5206\u9875\u67e5\u8be2\u4f18\u5316\u7b49\u3002\u6211\u5c06\u6f14\u793a\u4e24\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u4e00\u79cd\u662f\u4f7f\u7528 <code>ROWNUM<\/code>\uff0c\u53e6\u4e00\u79cd\u662f\u4f7f\u7528 <code>OFFSET FETCH<\/code> \u5b50\u53e5\u3002<\/p>\n<p><strong>\u4f7f\u7528 ROWNUM \u5b9e\u73b0\u5206\u9875\uff1a<\/strong><\/p>\n<pre><code class=\"language-sql\">SELECT *\nFROM (\n    SELECT your_columns, ROWNUM AS rnum\n    FROM your_table\n    WHERE ROWNUM &lt;= :upper_bound\n)\nWHERE rnum &gt;= :lower_bound;\n<\/code><\/pre>\n<p>\u8fd9\u91cc <code>:lower_bound<\/code> \u548c <code>:upper_bound<\/code> \u5206\u522b\u8868\u793a\u4f60\u60f3\u8981\u83b7\u53d6\u7684\u7ed3\u679c\u7684\u4e0b\u9650\u548c\u4e0a\u9650\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u5173\u952e\u662f\u4f7f\u7528\u5185\u90e8\u67e5\u8be2\u5728\u7ed3\u679c\u96c6\u4e2d\u4e3a\u6bcf\u4e00\u884c\u5206\u914d\u4e00\u4e2a\u884c\u53f7\uff0c\u5e76\u7136\u540e\u901a\u8fc7\u5916\u90e8\u67e5\u8be2\u9009\u62e9\u6307\u5b9a\u8303\u56f4\u7684\u884c\u3002<\/p>\n<p><strong>\u4f7f\u7528 OFFSET FETCH \u5b9e\u73b0\u5206\u9875\uff1a<\/strong><\/p>\n<pre><code class=\"language-sql\">SELECT your_columns\nFROM your_table\nORDER BY your_order_column\nOFFSET :offset ROWS FETCH NEXT :fetch_rows ROWS ONLY;\n<\/code><\/pre>\n<p>\u8fd9\u91cc <code>:offset<\/code> \u8868\u793a\u4ece\u7ed3\u679c\u96c6\u7684\u54ea\u4e00\u884c\u5f00\u59cb\uff0c<code>:fetch_rows<\/code> \u8868\u793a\u8981\u83b7\u53d6\u7684\u884c\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e Oracle 12c \u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/p>\n<p><strong>\u4f7f\u7528\u5b58\u50a8\u8fc7\u7a0b\u5b9e\u73b0\u5206\u9875\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b58\u50a8\u8fc7\u7a0b\u793a\u4f8b\uff0c\u4f7f\u7528 <code>ROWNUM<\/code> \u5b9e\u73b0\u5206\u9875\uff1a<\/p>\n<pre><code class=\"language-sql\">CREATE OR REPLACE PROCEDURE GetPagedData (\n    p_lower_bound IN NUMBER,\n    p_upper_bound IN NUMBER\n) AS\nBEGIN\n    FOR rec IN (\n        SELECT your_columns, ROWNUM AS rnum\n        FROM your_table\n        WHERE ROWNUM &lt;= p_upper_bound\n    )\n    WHERE rec.rnum &gt;= p_lower_bound\n    LOOP\n        -- Process the row or do something with the data\n        DBMS_OUTPUT.PUT_LINE(rec.your_columns);\n    END LOOP;\nEND GetPagedData;\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u5b58\u50a8\u8fc7\u7a0b\u4e2d\uff0c<code>p_lower_bound<\/code> \u548c <code>p_upper_bound<\/code> \u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u8981\u83b7\u53d6\u7684\u7ed3\u679c\u8303\u56f4\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u4e1a\u52a1\u9700\u6c42\u8fdb\u884c\u9002\u5f53\u7684\u8c03\u6574\u548c\u6539\u8fdb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Oracle \u4e2d\uff0c\u5b9e\u73b0\u5206\u9875\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2d\u4e00\u4e9b\u5e38\u89c1\u7684\u5305\u62ec\u4f7f\u7528 ROWNUM\u3001OFFSET FETCH \u5b50\u53e5 [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[120],"tags":[],"class_list":["post-15952","post","type-post","status-publish","format-standard","hentry","category-oracel"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/15952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=15952"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/15952\/revisions"}],"predecessor-version":[{"id":15953,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/15952\/revisions\/15953"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=15952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=15952"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=15952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}