Oracle:Cursor中的游标

时间:2010-01-27 16:11:07

标签: plsql

我对PL / SQL不熟悉,需要快速帮助。整个光标处理令人不安,但我已经习惯了。请仅关注游标1)cur_cheese_proprietary和2)cur_cheese_mms。我正在尝试从cur_cheese_mms中检索那些行,这些行与迭代时光标cur_cheese_proprietary当前行中的cheeseNumber具有相同的cheeseNumber。我希望在excel中打开一个CSV字符串,如下所示:

我还要列出所有那些仅存在于cur_cheese_proprietary或cur_cheese_mms中的行(其代码完全在pl / sql的下一个块中存在)

示例文件:开始

col1 col2 col3 ......(来自cur_cheese_proprietary)

'CHOM DATA'

col1 col2 col3(来自cur_cheese_mms)

仅在cur_cheese_proprietary中出现 col1 col2 col3

只在cur_cheese_mms出现 col1 col2 col3

示例文件:结束

不用多说,这就是我目前所拥有的。

问题1)在何处以及如何从ROWTYPE变量cur_rec_cheese中填充v_cheeseNumber cur_cheese_proprietary%ROWTYPE;?请看下面的代码。

问题2)如何找到不匹配的行(仅基于任一表中的列v_cheeseNumber)?

如果你有时间,请回答问题1。在阅读了更多的pl / sql语法之后,我或许可以弄清楚问题2。我猜外连接会起作用 - 意味着更多的游标!

CREATE OR REPLACE PROCEDURE sp_monthly_status
IS
   v_from        VARCHAR2 (80);
   v_subject     VARCHAR2 (80);
   crlf          VARCHAR2 (2);
   v_mail_host   VARCHAR2 (80);
   v_event_csv   VARCHAR2 (32767);
   v_cheese_csv   VARCHAR2 (32767);
   v_user_csv    VARCHAR2 (32767);
   v_recipient   VARCHAR2 (2000);
   v_rcpts       VARCHAR2 (2000);
   v_to          VARCHAR2 (100);
   v_error       VARCHAR2 (2000);
   n_event_count NUMBER;
   n_cheese_count NUMBER;
   n_user_count  NUMBER;
   n_pos         NUMBER;
   v_mail_conn   UTL_SMTP.connection;
   v_cheeseNumber VARCHAR2 (256);

   CURSOR cur_event_log IS
     SELECT REPLACE (l.evt_name, ',', ' ') || ',' ||
            REPLACE (l.evt_params, ',', ' ') || ',' ||
            l.evt_user || ',' || 
            TO_CHAR (l.evt_start_time, 'MM/DD/YYYY HH24:MI:SS') evt_info
       FROM chom_event_log l
      WHERE UPPER (l.evt_type) = 'REPORT'
        AND l.evt_start_time > ADD_MONTHS (SYSDATE, -1);

   CURSOR cur_cheese_proprietary IS     
     SELECT NVL (TO_CHAR (p.data_name), '<blank>') || ',' ||
            NVL (TO_CHAR (ls.external_data_id), '<blank>') || ',' ||
            NVL (TO_CHAR (ls.block_number), '<blank>') || ',' ||
            NVL (TO_CHAR (ls.TikTok), '<blank>') || ',' ||
            NVL (TO_CHAR (ls.TikTok_area), '<blank>') || ',' ||
            NVL (TO_CHAR (ls.cheese_number), '<blank>') || ',' ||
            NVL (TO_CHAR (ls.sbu), '<blank>') || ',' ||
            NVL (TO_CHAR (ls.comments), '<blank>') || ',' ||
            NVL (TO_CHAR (le.company_name), '<blank>') || ',' ||
            NVL (TO_CHAR (le.TikTok_percent), '<blank>')
            cheese_prop_data
       FROM chom_cheese_proprietary ls, chom_lessee_proprietary le, chom_data p
      WHERE ls.cheese_lessee_key = le.cheese_lessee_key
        AND ls.external_data_id = p.external_data_id
        AND ls.sbu = 'chom DW' 
   ORDER BY ls.sbu, p.data_name, block_number;

    CURSOR cur_cheese_mms IS
     SELECT NVL (TO_CHAR (p.data_name), '<blank>') || ',' ||
            NVL (TO_CHAR (ls.external_data_id), '<blank>') || ',' ||
            NVL (TO_CHAR (ls.block_number), '<blank>') || ',' ||
            NVL (TO_CHAR (ls.TikTok), '<blank>') || ',' ||
            NVL (TO_CHAR (ls.TikTok_area), '<blank>') || ',' ||
            NVL (TO_CHAR (ls.cheese_number), '<blank>') || ',' ||
            NVL (TO_CHAR (ls.sbu), '<blank>') || ',' ||
            NVL (TO_CHAR (ls.comments), '<blank>') || ',' ||
            NVL (TO_CHAR (le.company_name), '<blank>') || ',' ||
            NVL (TO_CHAR (gles.TikTok_percent), '<blank>')
            cheese_mms_data
       FROM chom_cheese_proprietary ls, chom_lessee_proprietary le, chom_data p, explore.chom_cheese gl, chom_lessee gles
      WHERE ls.cheese_lessee_key = le.cheese_lessee_key
        AND ls.external_data_id = p.external_data_id
        --AND gl.TikTok(+) = ls.TikTok
        AND gl.TikTok(+) = ls.TikTok
        --AND gl.cheese_NUMBER = gles.cheese_NUMBER
        AND gles.cheese_NUMBER=v_cheeseNumber
        AND ls.sbu = 'chom DW' 
   ORDER BY ls.sbu, p.data_name, ls.block_number;



   CURSOR cur_user_list IS
     SELECT DISTINCT l.evt_user
                FROM chom_event_log l
               WHERE l.evt_start_time > ADD_MONTHS (SYSDATE, -1)
                 AND UPPER (l.evt_name) = 'LOGIN INFORMATION';

   cur_rec       cur_event_log%ROWTYPE;
   cur_rec_cheese cur_cheese_proprietary%ROWTYPE;
   cur_rec_mms cur_cheese_mms%ROWTYPE;
   cur_rec_user  cur_user_list%ROWTYPE;


   -- Adds recipients for each comma separated values
   PROCEDURE sp_chom_add_recipient (p_rcpts VARCHAR2)
   IS
   BEGIN
      v_rcpts := p_rcpts;

      LOOP
         IF v_rcpts IS NULL
         THEN
            EXIT;
         END IF;

         n_pos := INSTR (v_rcpts, ',');

         IF n_pos > 0
         THEN
            v_to := TRIM (SUBSTR (v_rcpts, 1, n_pos - 1));
            v_rcpts := SUBSTR (v_rcpts, n_pos + 1);

            IF LENGTH (v_to) > 0
            THEN
               UTL_SMTP.rcpt (v_mail_conn, v_to);
            END IF;
         ELSE
            v_to := TRIM (v_rcpts);
            EXIT;
         END IF;
      END LOOP;

      IF LENGTH (v_to) > 0
      THEN
         UTL_SMTP.rcpt (v_mail_conn, v_to);
      END IF;
   END;

-- Begin of main procedure   
BEGIN

   -- Initiallize parameters
   v_from := 'stackoverflow@segfault.com';
   v_subject := 'chom Monthly Report';
   crlf := CHR (13) || CHR (10);
   n_event_count := 0;
   n_cheese_count := 0;
   n_user_count := 0;

   SELECT value INTO v_mail_host
     FROM chom_config_param
    WHERE UPPER (name) = 'SMTP_MAIL_HOST';

    SELECT value INTO v_recipient
     FROM chom_config_param
    WHERE UPPER (name) = 'MONTHLY_REPORT_MAIL_LIST';

   -- Header row for CSV file
   v_event_csv := 'Name,Description,User,Date Time' || crlf;
   v_cheese_csv := 'data Name,data ID,Block Number,Star Block,Protraction Area,cheese Number,SBU,Comments,Company Name,Lessee Percent' || crlf;
   v_user_csv := 'User CAI' || crlf;

   IF cur_event_log%ISOPEN THEN
      CLOSE cur_event_log;
      OPEN cur_event_log;
   ELSE
      OPEN cur_event_log;
   END IF;

   -- Create data in CSV format for chom event
   LOOP
      FETCH cur_event_log INTO cur_rec;
      EXIT WHEN cur_event_log%NOTFOUND;
      n_event_count := n_event_count + 1;
      v_event_csv := v_event_csv || cur_rec.evt_info || crlf;
   END LOOP;
   CLOSE cur_event_log;

   IF cur_cheese_proprietary%ISOPEN THEN
      CLOSE cur_cheese_proprietary;
      OPEN cur_cheese_proprietary;
   ELSE
      OPEN cur_cheese_proprietary;
   END IF;   


   -- Create data in CSV format 
   LOOP
      FETCH cur_cheese_proprietary INTO cur_rec_cheese;
      EXIT WHEN cur_cheese_proprietary%NOTFOUND;     
      n_cheese_count := n_cheese_count + 1;
      v_cheese_csv := v_cheese_csv || cur_rec_cheese.cheese_prop_data || crlf;
      v_cheese_csv := v_cheese_csv || 'CHOM DATA' || crlf;

      IF cur_cheese_mms%ISOPEN THEN
      CLOSE cur_cheese_mms;
      OPEN cur_cheese_mms;
   ELSE
      OPEN cur_cheese_mms;
   END IF;
   LOOP
      FETCH cur_cheese_mms INTO cur_rec_mms;
      EXIT WHEN cur_cheese_mms%NOTFOUND;
      n_cheese_count := n_cheese_count + 1;      
      v_cheese_csv := v_cheese_csv || cur_rec_mms.cheese_mms_data || crlf;
      END LOOP;
   END LOOP;   

   CLOSE cur_cheese_proprietary;   


   IF cur_user_list%ISOPEN THEN
      CLOSE cur_user_list;
      OPEN cur_user_list;
   ELSE
      OPEN cur_user_list;
   END IF;

   -- Create data in CSV format for USER list
   LOOP
      FETCH cur_user_list INTO cur_rec_user;
      EXIT WHEN cur_user_list%NOTFOUND;
      n_user_count := n_user_count + 1;
      v_user_csv := v_user_csv || cur_rec_user.evt_user || crlf;
   END LOOP;
   CLOSE cur_user_list;

   v_mail_conn := UTL_SMTP.open_connection (v_mail_host, 25);
   UTL_SMTP.helo (v_mail_conn, v_mail_host);
   UTL_SMTP.mail (v_mail_conn, v_from);

   -- email recipient
   sp_chom_add_recipient(v_recipient);

   UTL_SMTP.OPEN_DATA(v_mail_conn);
   -- Use MIME mail header
   UTL_SMTP.WRITE_DATA(v_mail_conn, 'Date: ' || TO_CHAR (SYSDATE, 'Dy, DD Mon YYYY hh24:mi:ss') || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, 'From: ' || v_from || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, 'Subject: ' || v_subject || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, 'To: ' || v_recipient || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, 'MIME-Version: 1.0' || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, 'Content-Type: multipart/mixed; boundary="chom_SEC_BOUND"' || crlf || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, 'This is multipart message' || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, '--chom_SEC_BOUND' || crlf);

   -- Message body 
   UTL_SMTP.WRITE_DATA(v_mail_conn, 'Content-Type: text/plain;' || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, 'Content-Transfer_Encoding: 7bit' || crlf || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, 'Hello User,' || crlf);
   IF n_event_count = 0 AND n_cheese_count = 0 AND n_user_count = 0 THEN
       UTL_SMTP.WRITE_DATA(v_mail_conn, '   No information available.' || crlf);
   ELSE
      UTL_SMTP.WRITE_DATA(v_mail_conn, '   Attached pcheese find chom data .' || crlf);
   END IF;
   UTL_SMTP.WRITE_DATA(v_mail_conn, ' ' || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, 'Thanks' || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, ' Support Team' || crlf);

   -- Attachment section chom Event log
   UTL_SMTP.WRITE_DATA(v_mail_conn, '--chom_SEC_BOUND' || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, 'Content-Type: text/plain;' || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, ' name="excel.csv"' || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, 'Content-Transfer_Encoding: 8bit' || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, 'Content-Disposition: attachment;' || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, ' filename="chom_Monthly_Report.csv"' || crlf || crlf);
   IF n_event_count > 0 THEN    
      -- Start CSV content of attachment
      UTL_SMTP.WRITE_DATA(v_mail_conn, v_event_csv);
      -- End CSV content of attachment
   ELSE
      UTL_SMTP.WRITE_DATA(v_mail_conn, 'No information available.' || crlf);
   END IF;

   -- Attachment section cheese proprietary
   UTL_SMTP.WRITE_DATA(v_mail_conn, '--chom_SEC_BOUND' || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, 'Content-Type: text/plain;' || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, ' name="excel.csv"' || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, 'Content-Transfer_Encoding: 8bit' || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, 'Content-Disposition: attachment;' || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, ' filename="cheese_Proprietary.csv"' || crlf || crlf);
   IF n_cheese_count > 0 THEN
      -- Start CSV content of attachment
      UTL_SMTP.WRITE_DATA(v_mail_conn, v_cheese_csv);
      -- End CSV content of attachment
   ELSE
      UTL_SMTP.WRITE_DATA(v_mail_conn, 'No information available.' || crlf);
   END IF;

   -- Attachment section user access list
   UTL_SMTP.WRITE_DATA(v_mail_conn, '--chom_SEC_BOUND' || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, 'Content-Type: text/plain;' || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, ' name="excel.csv"' || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, 'Content-Transfer_Encoding: 8bit' || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, 'Content-Disposition: attachment;' || crlf);
   UTL_SMTP.WRITE_DATA(v_mail_conn, ' filename="User_Access_List.csv"' || crlf || crlf);
   IF n_cheese_count > 0 THEN
      -- Start CSV content of attachment
      UTL_SMTP.WRITE_DATA(v_mail_conn, v_user_csv);
      -- End CSV content of attachment
   ELSE
      UTL_SMTP.WRITE_DATA(v_mail_conn, 'No information available.' || crlf);
   END IF;

   -- End MIME mail
   UTL_SMTP.WRITE_DATA(v_mail_conn, crlf || crlf || '--chom_SEC_BOUND--');
   UTL_SMTP.CLOSE_DATA(v_mail_conn);
   UTL_SMTP.QUIT (v_mail_conn);

   sp_chom_email_status('Monthly report sent successfully.');

EXCEPTION
   WHEN OTHERS
   THEN
      v_error := TO_CHAR (SQLCODE) || ': ' || SQLERRM;

      INSERT INTO chom_event_log
                  (evt_id, evt_source, evt_type,
                   evt_name, evt_user, evt_start_time, evt_end_time,
                   evt_params
                  )
           VALUES (chom_seq_evt_id.NEXTVAL, 'SP_chom_EMAIL_STATUS', 'Error',
                   'SP_chom_EMAIL_STATUS', 'JOB', SYSDATE, SYSDATE,
                   v_error
                  );

      sp_chom_email_status('Error in sending monthly report.' ||
            crlf || SQLCODE || ': ' || SQLERRM);
END sp_chom_monthly_status
/

1 个答案:

答案 0 :(得分:1)

我不太明白问题1,但我会尝试......

cur_cheese_mns光标使用v_cheeseNumber。因此,在打开v_cheeseNumber游标之前,必须为cur_cheese_mns分配值

您声称此值取自cur_rec_cheesecur_cheese_proprietary光标获取的值。 cur_cheese_proprietary光标没有任何列显示包含您的cheeseNumber。您需要修改光标以返回此列。

-- Create data in CSV format 
LOOP
   FETCH cur_cheese_proprietary INTO cur_rec_cheese;
   EXIT WHEN cur_cheese_proprietary%NOTFOUND;     
   n_cheese_count := n_cheese_count + 1;
   v_cheese_csv := v_cheese_csv || cur_rec_cheese.cheese_prop_data || crlf;
   v_cheese_csv := v_cheese_csv || 'CHOM DATA' || crlf; 

   /*
    * You will have to modify the cur_rec_cheese cursor to select the 
    * cheese number.
    */
   v_cheeseNumber := cur_rec_cheese.cheese_number;

   IF cur_cheese_mms%ISOPEN THEN
       CLOSE cur_cheese_mms;
       OPEN cur_cheese_mms;
   ELSE
       OPEN cur_cheese_mms;
   END IF;

   LOOP
       FETCH cur_cheese_mms INTO cur_rec_mms;
       EXIT WHEN cur_cheese_mms%NOTFOUND;
       n_cheese_count := n_cheese_count + 1;      
       v_cheese_csv := v_cheese_csv || cur_rec_mms.cheese_mms_data || crlf;
   END LOOP;
END LOOP;