Saturday, February 15, 2014

Function to get the list of Sting with covered with Quotes for individual columns in the List.

CREATE OR REPLACE FUNCTION get_not_in_clause (
   p_list_of_columns                    IN   VARCHAR2
)
   RETURN VARCHAR2
IS
   ---------------------------------------------------------------------------------
   -- Created By : Vijaykumar
   -- Creation Date :  15-Feb-2014
   -- purpose : purpose of this function is to split the string which is comma or any other
   --           separator and return the string with inverted quote string for each column in
   --          parameter list.
   --          ex :- 'CREATED_BY,CREATION_DATE,LAST_UPDATE_DATE,LAST_UPDATED_BY'
   --          the output of the this function will as below
   --          'CREATED_BY','CREATION_DATE','LAST_UPDATE_DATE','LAST_UPDATED_BY'
   -------------------------------------------------------------------------------
   l_count                                      NUMBER;
   l_string                                     VARCHAR2 (50);
   l_concatenated_string                        VARCHAR2 (2000);
BEGIN
   ------------------------------------------------------------------------
   --- below select statement is used to  Get the total count of Commas in
   --- the parameter string and adding 1 to consider the last column in the
   --- parameter list
   ------------------------------------------------------------------------
   BEGIN
      SELECT (LENGTH (REGEXP_REPLACE (p_list_of_columns, '[^,]')) / LENGTH (',')) + 1
        INTO l_count
        FROM DUAL;
   END;

   DBMS_OUTPUT.put_line ('Count Of Comma occurenece : ' || l_count);

   IF l_count IS NULL
   THEN
      l_concatenated_string                          := '''' || p_list_of_columns || '''';
   ELSE
      l_count                                        := l_count;
      DBMS_OUTPUT.put_line ('Count Of Comma occurenece 1: ' || l_count);

      FOR i IN 1 .. l_count
      LOOP
         DBMS_OUTPUT.put_line ('i : ' || i);

         SELECT REGEXP_SUBSTR (p_list_of_columns
                             , '[^,]+'
                             , 1
                             , i
                              )
           INTO l_string
           FROM DUAL;

         DBMS_OUTPUT.put_line ('String : ' || l_string);

         IF l_concatenated_string IS NULL
         THEN
            l_concatenated_string                          := '''' || l_string || '''';
         ELSE
            l_concatenated_string                          := l_concatenated_string || ',' || '''' || l_string || '''';
         END IF;
      END LOOP;
   END IF;

   RETURN l_concatenated_string;
EXCEPTION
   WHEN OTHERS
   THEN
      DBMS_OUTPUT.put_line ('UNexpected Error : ' || SUBSTR (SQLERRM
                                                           , 1
                                                           , 250
                                                            ));
      RETURN NULL;
END;

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.