Advertisement

07.20.2005 at 08:59PM PDT, ID: 21499118
[x]
Attachment Details

Rewrite and improve stored procedure - produced by conversion tool from SQL Server.

[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.4
Tags:

drop, procedure, rct1, stored

I have the following procedure that is used for returning paged resultsets using different methods (AND can be used with LARGE tables of data).
It is extremely simple concept:

 * PageNumber=1, PageSize=10, SearchTerm='hello' - would return the first page of 10 records that contain the word 'hello'.

This concept is used ALOT with web applications (browsing pages with <<prev || next >> buttons).

OK, so now I simply want the following procedure rewritten with the following in mind:

  * Remove and duplication or errors
  * Is there a way to remove the requirement of temporary table use with the ROWCOUNT|SUBQUERY method?
  * Optimise

--------------------------------------------------------------

BEGIN
EXECUTE IMMEDIATE 'DROP TABLE tmpNot CASCADE CONSTRAINTS';
EXCEPTION WHEN OTHERS THEN NULL;
END;
/


CREATE GLOBAL TEMPORARY TABLE tmpNot
     ON COMMIT PRESERVE ROWS AS
SELECT PICKDETAILKEY
FROM  PICKDETAIL
WHERE      1  = 2
/


BEGIN
EXECUTE IMMEDIATE 'DROP TABLE tmpIn CASCADE CONSTRAINTS';
EXCEPTION WHEN OTHERS THEN NULL;
END;
/


CREATE GLOBAL TEMPORARY TABLE tmpIn
     ON COMMIT PRESERVE ROWS AS
SELECT PICKDETAILKEY
FROM  PICKDETAIL
WHERE      1  = 2
/


CREATE GLOBAL TEMPORARY TABLE PKTABLE (PK1 VARCHAR2(1)  NOT NULL  PRIMARY KEY)
ON COMMIT PRESERVE ROWS
/


CREATE OR REPLACE PROCEDURE spGetPagedPickDetails
(
     PageNumber                 IN  OUT      INT             DEFAULT 1,
     PageSize                   IN  OUT      INT            DEFAULT 10,
     SearchText                 IN           VARCHAR2       DEFAULT NULL,
      
     Cur_Out                    IN  OUT      RCT1
)
AS
      -- create likeText used for searching
     likeText                   VARCHAR2(1);
      
      -- determine paging variables
     intStartRow                INT;
     PrimaryKey                 VARCHAR2(1);       
     
    CURSOR  PagingCursor      
       IS
 
            SELECT
                     PICKDETAILKEY
            FROM  
                    PICKDETAIL
            ORDER BY
                    PICKDETAILKEY;            
     
    CURSOR  PagingCursor
    IS
            SELECT
                     PICKDETAILKEY
            FROM  
                     PICKDETAIL
            WHERE      
                     ORDERKEY  LIKE likeText
                      OR     STORERKEY  LIKE likeText
                      OR     SKU  LIKE likeText
            ORDER BY
                    PICKDETAILKEY;
                    
BEGIN  

      -- default page if bad input
     IF PageNumber < 1 THEN      
           PageNumber  :=  1;
           NULL;
     END IF;
      
       -- get the like text if search term provided
     IF SearchText IS NOT NULL THEN      
           likeText  :=  '%' || SearchText || '%';
           NULL;
     END IF;       
      
      -- use "SubQuery" method for close pages
     IF( PageNumber >= 1 AND PageNumber < 20) THEN  
     BEGIN  
           -- works faster to just get TOP rows
          IF( PageNumber = 1) THEN  
          BEGIN
               
               IF( SearchText IS NULL) OR LENGTH(RTRIM(SearchText)) = 0 THEN  
               BEGIN
                   
                    OPEN RCT1 FOR
                    SELECT
                            PICKDETAILKEY,
                          STORERKEY,
                          SKU
                    FROM  
                                      PICKDETAIL
                    ORDER BY
                                      PICKDETAILKEY;
               END;
                    
               ELSE  
               BEGIN
                   
                    OPEN RCT1 FOR
                    SELECT
                          PICKDETAILKEY,
                          STORERKEY,
                          SKU
                    FROM  
                                      PICKDETAIL
                    WHERE      
                                      ORDERKEY  LIKE likeText
                                      OR     STORERKEY  LIKE likeText
                             OR     SKU  LIKE likeText
                    ORDER BY
                                      PICKDETAILKEY;
               END;
               END IF;
                      
          END;--END TOP METHOD
             
             
          ELSE  
          BEGIN  
               --SELECT * FROM PICKDETAIL  WHERE PICKDETAILKEY IN
               --     (SELECT TOP 10 PICKDETAILKEY FROM PICKDETAIL WHERE PICKDETAILKEY NOT IN
               --          (SELECT TOP 10 PICKDETAILKEY FROM PICKDETAIL ORDER BY GENNUMBER )  ORDER BY GENNUMBER )  ORDER BY GENNUMBER  
                    
                              
               -- no search subquery
               IF( SearchText IS NULL) THEN  
               BEGIN  
                   
                    EXECUTE IMMEDIATE 'TRUNCATE TABLE tmpNot';
                    INSERT INTO  
                                       tmpNot    
                    SELECT
                                       PICKDETAILKEY
                    FROM  
                                       PICKDETAIL
                    WHERE      
                                       ROWNUM  <=   PageSize
                    ORDER BY
                                       
                                       PICKDETAILKEY;
                                       
                   
                    EXECUTE IMMEDIATE 'TRUNCATE TABLE tmpIn';
                    INSERT INTO  
                                       tmpIn    
                    SELECT
                                       PICKDETAILKEY
                    FROM  
                                       PICKDETAIL
                    WHERE      
                                       PICKDETAILKEY  NOT IN ( SELECT * FROM  tmpNot )
                             AND     ROWNUM  <=   PageSize
                             --AND     ROWNUM  <=   PageSize
                    ORDER BY
                                       PICKDETAILKEY;
                                       
                   
                    OPEN RCT1 FOR
                    SELECT
                            PICKDETAILKEY,
                          STORERKEY,
                          SKU
                    FROM  
                                      PICKDETAIL
                    WHERE      
                                      PICKDETAILKEY  IN ( SELECT * FROM  tmpIn )
                    ORDER BY
                                      PICKDETAILKEY;
                              
                    /* drop temporary tables (shouldnt need to but check anyways)*/
                    EXECUTE IMMEDIATE ' DROP TABLE tmpIn, tmpNot ';
               END;
                    
               ELSE  
               BEGIN  
                   
                    EXECUTE IMMEDIATE 'TRUNCATE TABLE tmpNot';
                    INSERT INTO  
                                      tmpNot    
                    SELECT
                                      PICKDETAILKEY
                    FROM  
                                      PICKDETAIL
                    WHERE      
                                      (
                                      ORDERKEY  LIKE likeText
                            OR     STORERKEY  LIKE likeText
                            OR     SKU  LIKE likeText
                                      )
                            AND     ROWNUM  <=   PageSize
                    ORDER BY
                                      PICKDETAILKEY;
                   
                    EXECUTE IMMEDIATE 'TRUNCATE TABLE tmpIn';
                    INSERT INTO  
                                      tmpIn    
                    SELECT
                                      PICKDETAILKEY
                    FROM  
                                      PICKDETAIL
                    WHERE      
                                     (
                                     ORDERKEY  LIKE likeText
                           OR     STORERKEY  LIKE likeText
                           OR     SKU  LIKE likeText
                                     )
                           AND        PICKDETAILKEY  NOT IN ( SELECT * FROM  tmpNot )
                           AND     ROWNUM  <=   PageSize
                           --AND     ROWNUM  <=   PageSize
                    ORDER BY
                                     PICKDETAILKEY;
                                     
                   
                    OPEN RCT1 FOR
                    SELECT
                          PICKDETAILKEY,
                          STORERKEY,
                          SKU
                    FROM  
                                      PICKDETAIL
                    WHERE      
                                      PICKDETAILKEY  IN ( SELECT * FROM  tmpIn )
                    ORDER BY
                                      PICKDETAILKEY;
                                     
                    /* drop temporary tables (shouldnt need to but check anyways)*/
                    EXECUTE IMMEDIATE ' DROP TABLE tmpIn,tmpNot ' ;
                              
               END;
               END IF;
                    
          END;
          END IF; -- END SUBQUERY METHOD
             
     END; -- END TOP AND SUBQUERY METHODS
      
     ELSE  
     BEGIN  
            
          intStartRow  :=  ( ( PageNumber - 1 ) * PageSize + 1 );
             
           -- no searching required!
          IF( SearchText IS NULL) THEN  
             
          OPEN PagingCursor;
              FETCH  PagingCursor INTO PrimaryKey;
          WHILE  PageSize  >  0  AND  PagingCursor%FOUND
              LOOP              
          BEGIN
               
               INSERT INTO  
                                   PKTABLE ( PK1 )  
               VALUES          
                                ( PrimaryKey );
                                
                     FETCH  PagingCursor INTO PrimaryKey;
                          
               PageSize  :=  PageSize - 1;
                    
          END;          
          END LOOP;
               
           -- close and dereference cursors
          CLOSE PagingCursor;              
             
          /* return the paged set*/
          OPEN RCT1 FOR
          SELECT
                PICKDETAILKEY,
                STORERKEY,
                SKU
          FROM  
                          PICKDETAIL t,
                PKTABLE tt
          WHERE      
                          t.PICKDETAILKEY  = tt.PK1
          ORDER BY
                          PICKDETAILKEY;
     END;
     END IF;
        
        
END;
/
Answered By: twentyfour_seven
Expert Since: 05/11/2003
Accepted Solutions: 28
twentyfour_seven has been an Expert for 5 years 8 months, during which he has posted 343 comments and answered 28 questions. twentyfour_seven is just one of 1067 experts in the Oracle Database Zone. 4 experts collaborated on this answer, which was graded an "A" by the asker.
 
 
 
 
20081119-EE-VQP-48