{"id":1433,"date":"2015-06-08T10:02:53","date_gmt":"2015-06-08T09:02:53","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=1433"},"modified":"2015-06-08T10:18:57","modified_gmt":"2015-06-08T09:18:57","slug":"abap-how-to-find-a-gap-in-range-of-numbers","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=1433","title":{"rendered":"ABAP &#8211; How to find a gap in range of numbers"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"358\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=358\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" data-orig-size=\"44,50\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Picasa&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1365690880&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"SAP\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" class=\" size-full wp-image-358 alignleft\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" alt=\"SAP\" width=\"44\" height=\"50\" \/>In this article I&#8217;ll share a piece of code I use to find a gap (the first minimal number that does not exist) between the given MIX and MAX numbers. It is not an easy task like in other SQL languages so you have to apply a little bit of coding in ABAP.<!--more--><\/p>\n<p>In other SQL languages, (e.g. MySQL or Oracle) you can do something like the following SQL command:<\/p>\n<pre lang=\"SQL\">SELECT A-sequence + 1\r\n  FROM my_table AS A\r\n  LEFT OUTER JOIN my_table AS B \r\n    ON B-sequence = A-sequence + 1\r\n  WHERE B-sequence = NULL.<\/pre>\n<p>This &#8216;+ 1&#8217; is currently NOT possible in ABAP Open SQL, therefore you are not able to use such commands and you have to implement your own search algorithm.<\/p>\n<p>In the following piece of code I&#8217;ll show how to find the first unused material number in the MARA table.<\/p>\n<p>Please <strong>keep in mind<\/strong> that the presented approach (selecting all data to internal table and looping over it) has <strong>MUCH better<\/strong> performance than finding the same result using <strong>SELECT-ENDSELECT <\/strong>!!!<\/p>\n<pre lang=\"abap\">DATA:\r\n  lt_matnr    TYPE TABLE OF matnr,\r\n  lv_next_gap TYPE matnr,\r\n  lv_min      TYPE matnr VALUE 7000000,\r\n  lv_max      TYPE matnr VALUE 7999999.\r\n\r\n* Select all used numbers\r\nSELECT matnr\r\n  INTO TABLE lt_matnr\r\n  FROM mara\r\n  WHERE matnr BETWEEN lv_min AND lv_max\r\n  ORDER BY matnr ASCENDING.\r\n\r\n* Set counter to the minimal possible value\r\nlv_next_gap = lv_min.\r\nTRY.\r\n* While we are in the range &amp;&amp; number is used in the DB table\r\n    WHILE lv_next_gap          &lt;= lv_max AND\r\n          lt_matnr[ sy-index ] =  lv_next_gap.\r\n\r\n*     Increment our number which will be checked in the next loop\r\n*     Note: This increment can be substituted with more complex\r\n*           data manipulation in case the material number is not \r\n*           just a numeric value.\r\n      ADD 1 TO lv_next_gap.     \r\n\r\n    ENDWHILE.   \r\n\r\n* We stepped out of ITAB bounds\r\n  CATCH cx_sy_itab_line_not_found. \r\nENDTRY. \r\n\r\nIF lv_next_gap &gt; lv_max.\r\n  WRITE 'No gap available'.\r\nELSE.\r\n  WRITE lv_next_gap.\r\nENDIF.<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this article I&#8217;ll share a piece of code I use to find a gap (the first minimal number that does not exist) between the given MIX and MAX numbers. It is not an easy task like in other SQL &hellip; <a href=\"https:\/\/oprsteny.cz\/?p=1433\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"ABAP - How to find a gap in range of numbers","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[16,9],"tags":[435,434],"class_list":["post-1433","post","type-post","status-publish","format-standard","hentry","category-abap","category-development","tag-gap","tag-number-range"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-n7","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1433"}],"version-history":[{"count":3,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1433\/revisions"}],"predecessor-version":[{"id":1436,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1433\/revisions\/1436"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}