home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a524 / 37.ddi / demo / sample4.sql < prev    next >
Encoding:
Text File  |  1991-03-04  |  3.3 KB  |  101 lines

  1. rem
  2. rem $Header: sample4.sql,v 1002800.2 90/01/11 17:03:31 nsalah Exp $ sample4.sql Copyr (c) 1989 Oracle
  3. rem
  4. rem V6PLS10028,DISK$DEV2:[PLS1FREEZE.DEMO.10028]
  5. /*
  6. ** This program modifies the ACCOUNTS table based on instructions
  7. ** stored in the ACTION table.  Each row of the ACTION table 
  8. ** contains an account number to act upon, an action to be taken
  9. ** (insert, update, or delete), and an amount to update the
  10. ** account to if the action is not a delete.
  11. **      On an insert, if the account already exists, an update is
  12. ** performed instead.  On an update, if the account does not exist,
  13. ** then the account is created via an insert.  On a delete, if the 
  14. ** row does not exist, no alternative action is taken.
  15. **
  16. ** Copyright (c) 1989 by Oracle Corporation
  17. */
  18.  
  19. DECLARE
  20.     CURSOR c1 IS
  21.         SELECT account_id, oper_type, new_value FROM action
  22.         ORDER BY time_tag 
  23.     FOR UPDATE OF status;
  24.  
  25. BEGIN
  26.     FOR acct IN c1 LOOP        -- process each row one at a time
  27.  
  28.     acct.oper_type := upper(acct.oper_type);
  29.  
  30.     /*----------------------------------------*
  31.      * Process an UPDATE.  If the account to  *
  32.      * be updated doesn't exist, create a new *
  33.      * account.                  *
  34.      *----------------------------------------*/
  35.     IF acct.oper_type = 'U' THEN
  36.         UPDATE accounts SET bal = acct.new_value
  37.                 WHERE account_id = acct.account_id;
  38.  
  39.         IF SQL%NOTFOUND THEN     -- account didn't exist.  Create it.
  40.         INSERT INTO accounts
  41.                    VALUES (acct.account_id, acct.new_value);
  42.           UPDATE action SET status = 
  43.             'Update: ID not found. Value inserted.' 
  44.             WHERE CURRENT OF c1; 
  45.         ELSE
  46.         UPDATE action SET status = 'Update: Success.'
  47.             WHERE CURRENT OF c1;
  48.         END IF;
  49.  
  50.     /*--------------------------------------------*
  51.      * Process an INSERT.  If the account already *
  52.      * exists, do an update of the account        *
  53.      * instead.                      *
  54.      *--------------------------------------------*/
  55.     ELSIF acct.oper_type = 'I' THEN
  56.         BEGIN
  57.             INSERT INTO accounts
  58.             VALUES (acct.account_id, acct.new_value);
  59.             UPDATE action set status = 'Insert: Success.'
  60.             WHERE CURRENT OF c1;
  61.  
  62.         EXCEPTION
  63.             WHEN DUP_VAL_ON_INDEX THEN   -- account already exists
  64.             UPDATE accounts SET bal = acct.new_value
  65.                         WHERE account_id = acct.account_id;
  66.                    UPDATE action SET status = 
  67.                         'Insert: Acct exists. Updated instead.'
  68.                 WHERE CURRENT OF c1;
  69.         END;
  70.  
  71.     /*--------------------------------------------*
  72.      * Process a DELETE.  If the account doesn't  *
  73.      * exist, set the status field to say that    *
  74.      * the account wasn't found.              *
  75.      *--------------------------------------------*/
  76.     ELSIF acct.oper_type = 'D' THEN    
  77.         DELETE FROM accounts 
  78.         WHERE account_id = acct.account_id;
  79.  
  80.         IF SQL%NOTFOUND THEN   -- account didn't exist.
  81.             UPDATE action SET status = 'Delete: ID not found.'
  82.             WHERE CURRENT OF c1;
  83.         ELSE
  84.         UPDATE action SET status = 'Delete: Success.'
  85.             WHERE CURRENT OF c1;
  86.         END IF;
  87.  
  88.     /*--------------------------------------------*
  89.      * The requested operation is invalid.        *
  90.      *--------------------------------------------*/
  91.     ELSE        -- oper_type is invalid
  92.         UPDATE action SET status = 'Invalid operation. No action taken.'
  93.             WHERE CURRENT OF c1;
  94.  
  95.     END IF;
  96.  
  97.     END LOOP;
  98.     COMMIT;
  99. END;
  100. /
  101.