home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / programacao / visual / perl.exe / {app} / Templates / other / Sql.sql < prev    next >
Encoding:
Text File  |  2003-01-11  |  2.8 KB  |  83 lines

  1. DECLARE
  2.   CURSOR c1 IS
  3.     SELECT account_id, oper_type, new_value FROM action
  4.     ORDER BY time_tag
  5.     FOR UPDATE OF status;
  6.  
  7. BEGIN
  8.   FOR acct IN c1 LOOP  -- process each row one at a time
  9.  
  10.     acct.oper_type := upper(acct.oper_type);
  11.  
  12.     /*----------------------------------------*
  13.      * Process an UPDATE.  If the account to  *
  14.      * be updated doesn't exist, create a new *
  15.      * account.                               *
  16.      *----------------------------------------*/
  17.     IF acct.oper_type = 'U' THEN
  18.       UPDATE accounts SET bal = acct.new_value
  19.         WHERE account_id = acct.account_id;
  20.  
  21.       IF SQL%NOTFOUND THEN  -- account didn't exist. Create it.
  22.         INSERT INTO accounts
  23.           VALUES (acct.account_id, acct.new_value);
  24.         UPDATE action SET status =
  25.           'Update: ID not found. Value inserted.'
  26.           WHERE CURRENT OF c1;
  27.       ELSE
  28.         UPDATE action SET status = 'Update: Success.'
  29.           WHERE CURRENT OF c1;
  30.       END IF;
  31.  
  32.     /*--------------------------------------------*
  33.      * Process an INSERT.  If the account already *
  34.      * exists, do an update of the account        *
  35.      * instead.                                   *
  36.      *--------------------------------------------*/
  37.     ELSIF acct.oper_type = 'I' THEN
  38.       BEGIN
  39.         INSERT INTO accounts
  40.           VALUES (acct.account_id, acct.new_value);
  41.         UPDATE action set status = 'Insert: Success.'
  42.           WHERE CURRENT OF c1;
  43.       EXCEPTION
  44.         WHEN DUP_VAL_ON_INDEX THEN  -- account already exists
  45.           UPDATE accounts SET bal = acct.new_value
  46.             WHERE account_id = acct.account_id;
  47.           UPDATE action SET status =
  48.             'Insert: Acct exists. Updated instead.'
  49.             WHERE CURRENT OF c1;
  50.       END;
  51.  
  52.     /*--------------------------------------------*
  53.      * Process a DELETE.  If the account doesn't  *
  54.      * exist, set the status field to say that    *
  55.      * the account wasn't found.                  *
  56.      *--------------------------------------------*/
  57.     ELSIF acct.oper_type = 'D' THEN    
  58.       DELETE FROM accounts
  59.         WHERE account_id = acct.account_id;
  60.  
  61.       IF SQL%NOTFOUND THEN  -- account didn't exist.
  62.         UPDATE action SET status =
  63.           'Delete: ID not found.'
  64.           WHERE CURRENT OF c1;
  65.       ELSE
  66.         UPDATE action SET status = 'Delete: Success.'
  67.           WHERE CURRENT OF c1;
  68.       END IF;
  69.  
  70.     /*--------------------------------------------*
  71.      * The requested operation is invalid.        *
  72.      *--------------------------------------------*/
  73.     ELSE  -- oper_type is invalid
  74.       UPDATE action SET status =
  75.           'Invalid operation. No action taken.'
  76.           WHERE CURRENT OF c1;
  77.  
  78.     END IF;
  79.  
  80.   END LOOP;
  81.   COMMIT;
  82. END;
  83.