MOCKSTACKS
EN
Questions And Answers

More Tutorials









Oracle Collections and Records

Use a collection as a return type for a split function


It's necessary to declare the type; here t_my_list; a collection is a TABLE OF something

CREATE OR REPLACE TYPE t_my_list AS TABLE OF VARCHAR2(100);

Here's the function. Notice the () used as a kind of constructor, and the COUNT and EXTEND keywords that help you create and grow your collection;

CREATE OR REPLACE
FUNCTION cto_table(p_sep IN VARCHAR2, p_list IN VARCHAR2)
 RETURN t_my_list
AS
--- this function takes a string list, element being separated by p_sep
-- as separator
 l_string VARCHAR2(4000) := p_list || p_sep;
 l_sep_index PLS_INTEGER;
 l_index PLS_INTEGER := 1;
 l_tab t_my_list := t_my_list();
BEGIN
 LOOP
 l_sep_index := INSTR(l_string, p_sep, l_index);
 EXIT
 WHEN l_sep_index = 0;
 l_tab.EXTEND;
 l_tab(l_tab.COUNT) := TRIM(SUBSTR(l_string,l_index,l_sep_index - l_index));
 l_index := l_sep_index + 1;
 END LOOP;
 RETURN l_tab;
END cto_table;
/

Then you can see the content of the collection with the TABLE() function from SQL; it can be used as a list inside a SQL IN ( ..) statement:

SELECT * FROM A_TABLE
WHERE A_COLUMN IN ( TABLE(cto_table('|','a|b|c|d')) )
--- gives the records where A_COLUMN in ('a', 'b', 'c', 'd') --


Conclusion

In this page (written and validated by ) you learned about Oracle Collections and Records . What's Next? If you are interested in completing Oracle tutorial, your next topic will be learning about: Oracle Object Types.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.