{"id":336,"date":"2009-10-02T16:33:16","date_gmt":"2009-10-02T14:33:16","guid":{"rendered":"http:\/\/bar-solutions.com\/weblog\/?p=336"},"modified":"2009-10-02T16:33:16","modified_gmt":"2009-10-02T14:33:16","slug":"reading-values-from-a-varchar2-indexed-associative-array","status":"publish","type":"post","link":"https:\/\/blog.bar-solutions.com\/?p=336","title":{"rendered":"Reading values from a varchar2 indexed Associative Array"},"content":{"rendered":"<p align=\"left\"><a href=\"http:\/\/bar-solutions.com\/weblog\/wp-content\/logooracle.gif\"><img loading=\"lazy\" decoding=\"async\" style=\"border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px\" title=\"logo-oracle\" border=\"0\" alt=\"logo-oracle\" align=\"left\" src=\"http:\/\/bar-solutions.com\/weblog\/wp-content\/logooracle_thumb.gif\" width=\"124\" height=\"54\" \/><\/a><a href=\"http:\/\/www.oracle.com\" target=\"_blank\">Oracle<\/a> has provided us with collections since Oracle 7. It is one of the most renamed features in the Oracle database. In Oracle 7 they were called \u2018PL\/SQL Tables\u2019. In Oracle 8 they were renamed to \u2018Index By Tables\u2019 and since Oracle 9 they are called \u2018Associative Arrays\u2019. If you index by an integer value then you can read the values by using an integer index variable. If the array is dense, that means all the indexes, from the first to the last, have a value associated to them you can even use a numeric for loop to get to the data. But what if you index by a string value, which is a possibility since Oracle 9i. <\/p>\n<div align=\"left\"><\/div>\n<p><!--more-->  <\/p>\n<p align=\"left\"><a href=\"http:\/\/bar-solutions.com\/weblog\/wp-content\/9i.gif\"><img loading=\"lazy\" decoding=\"async\" style=\"border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px\" title=\"9i\" border=\"0\" alt=\"9i\" align=\"right\" src=\"http:\/\/bar-solutions.com\/weblog\/wp-content\/9i_thumb.gif\" width=\"57\" height=\"133\" \/><\/a>The solution is actually rather simple. Using a simple loop. First of all create an index variable of the same type you use to index the collection. It would be kind of silly to declare the collection using a varchar2(10) and then declare an index variable as a varchar2(5). If an index value would be \u2018Patrick\u2019 then this wouldn\u2019t fit in the index variable resulting in a error.<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c2f1561b-e301-4a9c-a68d-3f30bfdbc61a\" class=\"wlWriterEditableSmartContent\">\n<pre style=\"background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;;font-family:Courier New;font-size:12\"><span style=\"color: #000000;\">ORA<\/span><span style=\"color: #808080;\">-<\/span><span style=\"color: #800000; font-weight: bold;\">06502<\/span><span style=\"color: #000000;\">: PL<\/span><span style=\"color: #808080;\">\/<\/span><span style=\"color: #000000;\">SQL: numeric <\/span><span style=\"color: #808080;\">or<\/span><span style=\"color: #000000;\"> value error: <\/span><span style=\"color: #0000FF;\">character<\/span><span style=\"color: #000000;\"> string buffer too small<\/span><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<p align=\"left\">Then assign the index variable the value of the first index used. Now start the loop and do the stuff you want with the collection. At the end of the loop assign the next index value to the variable. If you try to read past the end of the collection, then the value of the index will be NULL, which is a great marker to exit the loop.<\/p>\n<p align=\"left\">If you put the exit clause as the first statement in the loop, then there is no reason to check for any items present in the collection. If you want to check how many items are present, then use &lt;collection&gt;.count.<\/p>\n<p align=\"left\">I have created a small script to demonstrate how it can be done:<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b728b05e-3749-479b-8c21-11fa583a0b95\" class=\"wlWriterEditableSmartContent\">\n<pre style=\"background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;;font-family:Courier New;font-size:12\"><span style=\"color: #0000FF;\">declare<\/span><span style=\"color: #000000;\">\n  subtype index_t <\/span><span style=\"color: #0000FF;\">is<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">varchar2<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #800000; font-weight: bold;\">10<\/span><span style=\"color: #000000;\">);\n  type  birthdates_tt <\/span><span style=\"color: #0000FF;\">is<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">table<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">of<\/span><span style=\"color: #000000;\"> date <\/span><span style=\"color: #0000FF;\">index<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">by<\/span><span style=\"color: #000000;\"> index_t;\n  l_birthdates birthdates_tt;\n  l_indx index_t;\n<\/span><span style=\"color: #0000FF;\">begin<\/span><span style=\"color: #000000;\">\n  <\/span><span style=\"color: #008080;\">--<\/span><span style=\"color: #008080;\"> Fill the table with the birthdates<\/span><span style=\"color: #008080;\">\n<\/span><span style=\"color: #000000;\">  l_birthdates(<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\">Patrick<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\">) :<\/span><span style=\"color: #808080;\">=<\/span><span style=\"color: #000000;\"> to_date(<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\">29-12-1972<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\">,<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\">DD-MM-YYYY<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\">);\n  l_birthdates(<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\">Dana<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\">) :<\/span><span style=\"color: #808080;\">=<\/span><span style=\"color: #000000;\"> to_date(<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\">06-03-1976<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\">,<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\">DD-MM-YYYY<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\">);\n  l_birthdates(<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\">Quinty<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\">) :<\/span><span style=\"color: #808080;\">=<\/span><span style=\"color: #000000;\"> to_date(<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\">18-12-1998<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\">,<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\">DD-MM-YYYY<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\">);\n  l_birthdates(<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\">Kayleigh<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\">) :<\/span><span style=\"color: #808080;\">=<\/span><span style=\"color: #000000;\"> to_date(<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\">19-11-2000<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\">,<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\">DD-MM-YYYY<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\">);\n  l_birthdates(<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\">Mitchell<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\">) :<\/span><span style=\"color: #808080;\">=<\/span><span style=\"color: #000000;\"> to_date(<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\">23-06-2003<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\">,<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\">DD-MM-YYYY<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\">);\n\n  <\/span><span style=\"color: #008080;\">--<\/span><span style=\"color: #008080;\"> make index variable the same as the first from the list<\/span><span style=\"color: #008080;\">\n<\/span><span style=\"color: #000000;\">  l_indx :<\/span><span style=\"color: #808080;\">=<\/span><span style=\"color: #000000;\"> l_birthdates.first;\n  <\/span><span style=\"color: #008080;\">--<\/span><span style=\"color: #008080;\"> start a simple loop<\/span><span style=\"color: #008080;\">\n<\/span><span style=\"color: #000000;\">  loop\n    <\/span><span style=\"color: #008080;\">--<\/span><span style=\"color: #008080;\"> exit the loop when we passed the end<\/span><span style=\"color: #008080;\">\n<\/span><span style=\"color: #000000;\">    <\/span><span style=\"color: #0000FF;\">exit<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">when<\/span><span style=\"color: #000000;\"> l_indx <\/span><span style=\"color: #0000FF;\">is<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">null<\/span><span style=\"color: #000000;\">;\n    <\/span><span style=\"color: #008080;\">--<\/span><span style=\"color: #008080;\"> write the value of the index and the value at index in the AA to screen<\/span><span style=\"color: #008080;\">\n<\/span><span style=\"color: #000000;\">    dbms_output.put_line(l_indx <\/span><span style=\"color: #808080;\">||<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\"> =&gt; <\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #808080;\">||<\/span><span style=\"color: #000000;\">l_birthdates(l_indx));\n    <\/span><span style=\"color: #008080;\">--<\/span><span style=\"color: #008080;\"> select the next index value from the list<\/span><span style=\"color: #008080;\">\n<\/span><span style=\"color: #000000;\">    l_indx :<\/span><span style=\"color: #808080;\">=<\/span><span style=\"color: #000000;\"> l_birthdates.<\/span><span style=\"color: #0000FF;\">next<\/span><span style=\"color: #000000;\">(l_indx);\n  <\/span><span style=\"color: #0000FF;\">end<\/span><span style=\"color: #000000;\"> loop;\n<\/span><span style=\"color: #0000FF;\">end<\/span><span style=\"color: #000000;\">;\n<\/span><span style=\"color: #808080;\">\/<\/span><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<p align=\"left\"><a href=\"http:\/\/bar-solutions.com\/weblog\/wp-content\/WebButton.jpg\"><img loading=\"lazy\" decoding=\"async\" style=\"border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px\" title=\"WebButton\" border=\"0\" alt=\"WebButton\" align=\"right\" src=\"http:\/\/bar-solutions.com\/weblog\/wp-content\/WebButton_thumb.jpg\" width=\"141\" height=\"264\" \/><\/a>I hope this information helps in leveraging the power of collections. That is no need to know the index values upfront, because you can traverse the collection at runtime where you don\u2019t know the values of the indexes used. Some of this behavior and more about collections will be part of my presentation about <a href=\"http:\/\/www.odtugopp.com\/presentations.html#optimizingcollections\">Optimizing SQL with Collections<\/a> at <a href=\"http:\/\/www.odtugopp.com\" target=\"_blank\">OPP2009<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Oracle has provided us with collections since Oracle 7. It is one of the most renamed features in the Oracle database. In Oracle 7 they were called \u2018PL\/SQL Tables\u2019. In Oracle 8 they were renamed to \u2018Index By Tables\u2019 and since Oracle 9 they are called \u2018Associative Arrays\u2019. If you index by an integer value [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,5],"tags":[],"class_list":["post-336","post","type-post","status-publish","format-standard","hentry","category-oracle","category-plsql"],"_links":{"self":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=336"}],"version-history":[{"count":0,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/336\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}