0

I am working on call center customer search query. If user enter last name and driving license I have to find out the most close matching customer(s) from database. I am using fuzzy search, soundex() to compare customer last name. Base on different conditions I have to keep using soundex function, it is effecting performance. I try to use With Clause but since I have many IF and ELSE parts it is giving table or view not found exception.

I tried to google to find how to use with the clause and with clause with function, none of them are helpful.

PROCEDURE RetrieveCustomer
(
      i_lastname          IN varchar,
      i_DL             IN varchar, 
      o_cursor            OUT     T_CURSOR
)  
IS
      v_DLAndExactLastNameCnt        Number; 
      v_DLAndLastNameCnt             Number;
      v_DLCnt                        Number;      
  BEGIN

     SELECT COUNT(*) INTO v_DLAndExactLastNameCnt
     FROM   t1, t2
     WHERE t1.DL = i_DL AND (upper(t2.last_name) like upper(i_lastname));

     IF(v_DLAndExactLastNameCnt > 0) THEN
        OPEN o_cursor FOR
            select
              a,
              b,
              'DL + ELN' as search_result_baseon --exact last name match
        from t1, t2, t3, t4
        where t1.DL = i_DL AND (upper(t2.last_name) like upper(i_lastname));

     ELSE
         SELECT COUNT(*) INTO v_DLAndLastNameCnt
         FROM   t1, t2
         WHERE t1.DL= i_DL
               AND p.last_name in (select last_name from t2 where soundex(last_name) = soundex(i_lastname));                                 

          IF(v_DLAndLastNameCnt > 0) THEN   
            OPEN o_cursor FOR                 
                select 
                       a,
                     b,
                     'DL + LN' as search_result_baseon -- last name not exact match
                from t1, t2, t3, t4 
                where
                     t1.DL= i_DL
                     AND p.last_name in (                          
                            select last_name from t2 where soundex(last_name) = soundex(i_lastname)
                                                );                                   
           ELSE
              SELECT COUNT(*) INTO v_DLCnt
               FROM   t1
               WHERE  t1.DL = i_DL;
               IF(v_DLCnt > 0) THEN
                  OPEN o_cursor FOR                 
                   select a
                              b,
                          'DL' as search_result_baseon 
                  from t1, t2, t3, t4
                  where t1.DL = i_DL;

                ELSE
                    OPEN o_cursor FOR                 
                        select 
                          a,
                          b,                          
                          'LN' as search_result_baseon 
                  from t1, t2, t3, t4
                  where t2.last_name IN ( 
                              select last_name from t2 where soundex(last_name) = soundex(i_lastname)
                           )
                 END IF;               
             END IF;                                                
       END IF;  

  END RetrieveCustomer;

Is there any way I can store matching last name record in some in-memory table and use it as and when require in that procedure? Can anyone of you please suggest me better way to write my procedure:

Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28
ksam
  • 59
  • 1
  • 7
  • 1
    You may be able to create a function-based-index to see if it improves performance. Please run an explain plan and edit the question and post results. – OldProgrammer Apr 03 '19 at 18:56
  • what is function-based-index? The last name is already an index field. when I run a query using soundex(), without the procedure, that also takes time. – ksam Apr 03 '19 at 19:32
  • @Pavel Smirnov, thank you for editing. I tried but was not able to format, add procedure header in the code section. how did you do that? – ksam Apr 03 '19 at 19:47
  • Just select the code and click the "Code Sample" button. There must be some space in the beginning of each line in order to be formatted properly. – Pavel Smirnov Apr 03 '19 at 20:55

0 Answers0