Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

using PL/SQL to detect if a table is empty

user10936714Jun 20 2009 — edited Jun 21 2009
I need to modify the following function to work with empty tables. I'd like it to return NULL if p_varchar2_tab does not have any rows:
CREATE OR REPLACE TYPE t_varchar2_tab AS TABLE OF VARCHAR2(4000);

CREATE OR REPLACE FUNCTION tab_to_string (p_varchar2_tab  IN  t_varchar2_tab,
                                          p_delimiter     IN  VARCHAR2 DEFAULT ',') RETURN VARCHAR2 IS
  l_string     VARCHAR2(32767);
BEGIN
  FOR i IN p_varchar2_tab.FIRST .. p_varchar2_tab.LAST LOOP
    IF i != p_varchar2_tab.FIRST THEN
      l_string := l_string || p_delimiter;
    END IF;
    l_string := l_string || p_varchar2_tab(i);
  END LOOP;
  RETURN l_string;
END tab_to_string;
This function comes from this article on string aggregation techniques: http://www.oracle-base.com/articles/10g/StringAggregationTechniques.php

Thanks!

Edited by: user10936714 on Jun 20, 2009 12:44 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 19 2009
Added on Jun 20 2009
8 comments
4,510 views