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

  1. #ifdef RCSID
  2. static char *RCSid = 
  3.    "$Header: examp7.pc,v 1002800.3 90/04/13 11:04:06 pgreenwa Exp $ examp7.pc Copyr (c) 1989 Oracle";
  4. #endif /* RCSID */
  5.  
  6. /* V6PLS10028,DISK$DEV2:[PLS1FREEZE.DEMO.10028] */
  7. /************************************************************************
  8.  *                                              *
  9.  *  EMBEDDED PL/SQL DEMO                                                *
  10.  *                                                                      *
  11.  *  This program asks for an account number, whether you want to     *
  12.  *  Debit or Credit the account, and how much you want to Debit or    *
  13.  *  Credit it by.  If the account is valid, a Credit will succeed.    *
  14.  *  A Debit will only succeed if the account is valied and there are    *
  15.  *  sufficient funds.
  16.  *                                    *
  17.  *  Copyright (c) 1989 by Oracle Corporation.                *
  18.  ************************************************************************/
  19.  
  20. #include <stdio.h>
  21.  
  22. EXEC SQL BEGIN DECLARE SECTION;
  23.     int     acct, amount;
  24.     varchar tran_type[10];
  25.     varchar status[65];
  26.     varchar uid[20];
  27.     varchar pwd[20];
  28. EXEC SQL END DECLARE SECTION;
  29.  
  30. EXEC SQL INCLUDE SQLCA;
  31.  
  32. main()
  33. {
  34.  
  35.     strcpy (uid.arr,"plsqa");
  36.     uid.len=strlen(uid.arr);
  37.     strcpy (pwd.arr,"supersecret");
  38.     pwd.len=strlen(pwd.arr);
  39.  
  40.     printf("\n\n\tEmbedded PL/SQL Demo\n\n");
  41.     printf("Trying to connect...");
  42.  
  43.     EXEC SQL WHENEVER SQLERROR GOTO errprint;
  44.  
  45.     EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
  46.     printf(" connected.\n");
  47.  
  48.  
  49.     for (;;)  /* loop infinitely */
  50.         {
  51.  
  52.         printf("\n\n** What is the account number? (-1 to end)  ");
  53.         scanf("%d", &acct);
  54.         if (acct == -1)
  55.             exit(0);         /* End program if acct is -1 */
  56.  
  57.         printf("\n   What type of transaction? (C)redit or (D)ebit   ");
  58.         scanf("%s", tran_type.arr);
  59.         tran_type.len = 1;   /* Only want the first character */
  60.  
  61.         printf("\n   What is the transaction amount? (Whole dollars only)  ");
  62.         scanf("%d", &amount);
  63.  
  64.         /* ---------------------------------- */
  65.         /* ----- Begin the PL/SQL block ----- */
  66.         /* ---------------------------------- */
  67.         EXEC SQL EXECUTE
  68.  
  69.         DECLARE
  70.             old_bal     NUMBER(11,2);
  71.             no_account  EXCEPTION;
  72.         BEGIN
  73.             :tran_type := UPPER(:tran_type);
  74.             IF :tran_type = 'C' THEN       -- credit the account
  75.                 UPDATE accounts SET bal = bal + :amount
  76.                     WHERE account_id = :acct;
  77.                 IF SQL%ROWCOUNT = 0 THEN    -- no rows affected
  78.                     RAISE no_account;
  79.                 ELSE
  80.                     :status := 'Credit complete.';
  81.                 END IF;
  82.             ELSIF :tran_type = 'D' THEN    -- debit the account
  83.                 SELECT bal INTO old_bal FROM accounts
  84.                     WHERE account_id = :acct;
  85.                 IF old_bal >= :amount THEN      -- there's enough money
  86.                     UPDATE accounts SET bal = bal - :amount
  87.                         WHERE account_id = :acct;
  88.                     :status := 'Debit complete.';
  89.                 ELSE
  90.                     :status := 'Not enough funds.';
  91.                 END IF;
  92.             ELSE
  93.                 :status := :tran_type || ' is not a legal transaction.';
  94.             END IF;
  95.             COMMIT;
  96.         EXCEPTION
  97.             WHEN NO_DATA_FOUND OR no_account THEN
  98.                 :status := 'Account does not exist.';
  99.             WHEN OTHERS THEN
  100.                 :status := 'Error: ' || SQLERRM(SQLCODE);
  101.         END;
  102.  
  103.         END-EXEC;
  104.         /* -------------------------------- */
  105.         /* ----- End the PL/SQL block ----- */
  106.         /* -------------------------------- */
  107.  
  108.     status.arr[status.len] = '\0';       /* null terminate the string */
  109.     printf("\n\n   Status: %s", status.arr);
  110.     }  /* End of loop */
  111.  
  112. errprint:
  113.     EXEC SQL WHENEVER SQLERROR CONTINUE;
  114.     printf("\n\n>>>>> Error during execution:\n");
  115.     printf("%s\n",sqlca.sqlerrm.sqlerrmc);
  116.     EXEC SQL ROLLBACK RELEASE;
  117.     exit(1);
  118. }
  119.