	/*  
	
	reportblob.sql -	Create console-suitable report of CET_BADBLOB table, i.e. report the 'CORRUPT' items 

	Copyright 2007, IBM Corporation

	*/
/*
set serveroutput on size 1000000;
set echo off;
set linesize 120
set heading off
set line 10000
set wrap off
set newpage NONE
BEGIN 
  DBMS_OUTPUT.enable(1000000);
  report_cet_badblob(srcCtgId);
END; 
/
*/
CREATE PROCEDURE REPORT_CET_BADBLOB(IN srcCtgId INTEGER)
p1: BEGIN

        DECLARE my_cv_FOUND INTEGER DEFAULT NULL;
        DECLARE my_cv_count_FOUND INTEGER DEFAULT NULL;
        DECLARE SQLCODE INTEGER DEFAULT 0;
        DECLARE my_cv_ROWCOUNT INTEGER;
        BEGIN
  
        DECLARE view_rec_itm_primary_key VARCHAR(300);
         DECLARE sql_stmt VARCHAR(200);
          DECLARE sql_count_stmt VARCHAR(200);
         DECLARE rows_fetched INTEGER;
         DECLARE KEYS_TO_SHOW BIGINT DEFAULT 5;
         DECLARE my_cv_OPEN_FLAG INTEGER;
         DECLARE my_cv_ST1_STMT STATEMENT; 
         DECLARE my_cv_count_STMT STATEMENT; 
         DECLARE my_cv CURSOR WITH HOLD WITH RETURN TO CALLER FOR my_cv_ST1_STMT;
         DECLARE my_cv_count CURSOR WITH HOLD WITH RETURN TO CALLER FOR my_cv_count_STMT;

         DECLARE EXIT HANDLER FOR SQLEXCEPTION, NOT FOUND

         BEGIN
                DECLARE EXIT HANDLER FOR SQLSTATE '42710'
                BEGIN
                END;
          END;

         SET sql_stmt = 'SELECT cet_primary_key FROM CET_BADBLOB WHERE CET_SRC_CONTAINER_ID =' || srcCtgID || ' and CET_STATUS = ''CORRUPT''';
         SET sql_count_stmt = 'SELECT COUNT(*) FROM CET_BADBLOB WHERE CET_SRC_CONTAINER_ID =' || srcCtgID || 'CET_STATUS = ''CORRUPT''' ;

         PREPARE my_cv_count_STMT from sql_count_stmt ;
         OPEN my_cv_count ;

         BEGIN
                    DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_count_FOUND = SQLCODE;
                    FETCH FROM my_cv_count INTO rows_fetched;
        END;

        INSERT INTO MESSAGES (MESSAGE_D, COLLAB_AREA_ID,MESSAGE_TEXT) VALUES
(nextval for message_id_seq,null,CHAR('***TOTAL NUMBER OF BAD ITEMS FOUND = ')|| RTRIM(CHAR(rows_fetched))));
        COMMIT;
        CLOSE my_cv_count;

        SET my_cv_OPEN_FLAg = 1;
        SET my_cv_ROWCOUNT = 1;

        PREPARE my_cv_ST1_STMT FROM sql_stmt;
        OPEN my_cv;

        LOOP LABEL1:      LOOP
                BEGIN
                DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_FOUND = SQLCODE;
                SET my_cv_FOUND = 0;
                FETCH FROM my_cv INTO view_rec_itm_primary_key ;
                END; 

                IF my_cv_FOUND = 100 THEN
                    LEAVE LOOP_LABEL1;
                END IF;
                INSERT INTO MESSAGES (message_id, collab_area_id, message_text) VALUES(NEXTVAL FOR MESSAGE_ID_SEQ,NULL,CHAR('NEXT BAD ITEM : PRIMARY KEY')|| RTRIM(CHAR(view_rec_itm_primary_key)));
                COMMIT;

                SET my_cv_ROWCOUNT = my_cv_ROWCOUNT + 1;

                END LOOP LOOP_LABEL1;
                 SET my_cv_OPEN_FLAG = 0;
                 CLOSE my_cv;

        END;

 END p1 @
