home *** CD-ROM | disk | FTP | other *** search
- #ifdef RCSID
- static char *RCSid =
- "$Header: examp7.pc,v 1002800.3 90/04/13 11:04:06 pgreenwa Exp $ examp7.pc Copyr (c) 1989 Oracle";
- #endif /* RCSID */
-
- /* V6PLS10028,DISK$DEV2:[PLS1FREEZE.DEMO.10028] */
- /************************************************************************
- * *
- * EMBEDDED PL/SQL DEMO *
- * *
- * This program asks for an account number, whether you want to *
- * Debit or Credit the account, and how much you want to Debit or *
- * Credit it by. If the account is valid, a Credit will succeed. *
- * A Debit will only succeed if the account is valied and there are *
- * sufficient funds.
- * *
- * Copyright (c) 1989 by Oracle Corporation. *
- ************************************************************************/
-
- #include <stdio.h>
-
- EXEC SQL BEGIN DECLARE SECTION;
- int acct, amount;
- varchar tran_type[10];
- varchar status[65];
- varchar uid[20];
- varchar pwd[20];
- EXEC SQL END DECLARE SECTION;
-
- EXEC SQL INCLUDE SQLCA;
-
- main()
- {
-
- strcpy (uid.arr,"plsqa");
- uid.len=strlen(uid.arr);
- strcpy (pwd.arr,"supersecret");
- pwd.len=strlen(pwd.arr);
-
- printf("\n\n\tEmbedded PL/SQL Demo\n\n");
- printf("Trying to connect...");
-
- EXEC SQL WHENEVER SQLERROR GOTO errprint;
-
- EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
- printf(" connected.\n");
-
-
- for (;;) /* loop infinitely */
- {
-
- printf("\n\n** What is the account number? (-1 to end) ");
- scanf("%d", &acct);
- if (acct == -1)
- exit(0); /* End program if acct is -1 */
-
- printf("\n What type of transaction? (C)redit or (D)ebit ");
- scanf("%s", tran_type.arr);
- tran_type.len = 1; /* Only want the first character */
-
- printf("\n What is the transaction amount? (Whole dollars only) ");
- scanf("%d", &amount);
-
- /* ---------------------------------- */
- /* ----- Begin the PL/SQL block ----- */
- /* ---------------------------------- */
- EXEC SQL EXECUTE
-
- DECLARE
- old_bal NUMBER(11,2);
- no_account EXCEPTION;
- BEGIN
- :tran_type := UPPER(:tran_type);
- IF :tran_type = 'C' THEN -- credit the account
- UPDATE accounts SET bal = bal + :amount
- WHERE account_id = :acct;
- IF SQL%ROWCOUNT = 0 THEN -- no rows affected
- RAISE no_account;
- ELSE
- :status := 'Credit complete.';
- END IF;
- ELSIF :tran_type = 'D' THEN -- debit the account
- SELECT bal INTO old_bal FROM accounts
- WHERE account_id = :acct;
- IF old_bal >= :amount THEN -- there's enough money
- UPDATE accounts SET bal = bal - :amount
- WHERE account_id = :acct;
- :status := 'Debit complete.';
- ELSE
- :status := 'Not enough funds.';
- END IF;
- ELSE
- :status := :tran_type || ' is not a legal transaction.';
- END IF;
- COMMIT;
- EXCEPTION
- WHEN NO_DATA_FOUND OR no_account THEN
- :status := 'Account does not exist.';
- WHEN OTHERS THEN
- :status := 'Error: ' || SQLERRM(SQLCODE);
- END;
-
- END-EXEC;
- /* -------------------------------- */
- /* ----- End the PL/SQL block ----- */
- /* -------------------------------- */
-
- status.arr[status.len] = '\0'; /* null terminate the string */
- printf("\n\n Status: %s", status.arr);
- } /* End of loop */
-
- errprint:
- EXEC SQL WHENEVER SQLERROR CONTINUE;
- printf("\n\n>>>>> Error during execution:\n");
- printf("%s\n",sqlca.sqlerrm.sqlerrmc);
- EXEC SQL ROLLBACK RELEASE;
- exit(1);
- }
-