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

  1. #ifdef RCSID
  2. static char *RCSid = 
  3.    "$Header: examp6.pc,v 1002100.2 90/01/05 17:59:50 nsalah Exp $ examp6.pc Copyr (c) 1989 Oracle";
  4. #endif /* RCSID */
  5.  
  6. /* V6PLS10021,DISK$DEV9:[PLS.DEMO.10021] */
  7. /************************************************************************
  8.  *                                              *
  9.  *  EMBEDDED PL/SQL DEMO                                                *
  10.  *                                                                      *
  11.  *  This program prompts the user for the name of an employee.  It then *
  12.  *  executes a PL/SQL block which contains four SELECT statements in    *
  13.  *  order to obtain information on this employee.  The information      *
  14.  *  returned includes:  employee's job, the date the employee was       *
  15.  *  hired, the number of people who have served the company longer,     *
  16.  *  the employee's salary, the number of people in the company who have *
  17.  *  a higher salary, the employee's department number, and the total    *
  18.  *  number of people in that department.                 *
  19.  *                                    *
  20.  *  This program is intended to show the use of host variables in       *
  21.  *  PL/SQL.                                *
  22.  *                                    *
  23.  *  Copyright (c) 1989 by Oracle Corporation.                *
  24.  ************************************************************************/
  25.  
  26. #include <stdio.h>
  27.  
  28. EXEC SQL BEGIN DECLARE SECTION;
  29.     varchar empname[11];
  30.     varchar jobtype[9];
  31.     varchar hired[9];
  32.     int     salary;
  33.     int     dept;
  34.     int     worked_longer;
  35.     int     higher_sal;
  36.     int     total_in_dept;
  37.     varchar uid[20];
  38.     varchar pwd[20];
  39. EXEC SQL END DECLARE SECTION;
  40.  
  41. EXEC SQL INCLUDE SQLCA;
  42.  
  43. main()
  44. {
  45.  
  46.     /* Set up userid and password */
  47.     strcpy (uid.arr,"plsqa");
  48.     uid.len=strlen(uid.arr);
  49.     strcpy (pwd.arr,"supersecret");
  50.     pwd.len=strlen(pwd.arr);
  51.  
  52.     printf("\n\n\tEmbedded PL/SQL Demo\n\n");
  53.     printf("Trying to connect...");
  54.  
  55.     EXEC SQL WHENEVER SQLERROR GOTO errprint;
  56.  
  57.     /* Connect to ORACLE */
  58.     EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
  59.     printf(" connected.\n");
  60.  
  61.  
  62.     for (;;)  /* Loop infinitely */
  63.         {
  64.         printf("\n** Name of employee? (<return> to end)  ");
  65.         gets(empname.arr);                   /* Get the name      */
  66.         if (strlen(empname.arr) == 0)        /* No name entered,  */
  67.         {
  68.             EXEC SQL COMMIT WORK RELEASE;    /* so log off ORACLE */
  69.             exit(0);                         /* and exit program  */
  70.         }
  71.     empname.len = strlen(empname.arr);
  72.  
  73.         /* ---------------------------------- */
  74.         /* ----- Begin the PL/SQL block ----- */
  75.         /* ---------------------------------- */
  76.         EXEC SQL EXECUTE
  77.  
  78.         BEGIN
  79.         SELECT job, hiredate, sal, deptno INTO
  80.           :jobtype, :hired, :salary, :dept FROM emp
  81.           WHERE ename = UPPER(:empname);
  82.  
  83.         /* Get number of people whose length of *
  84.          * service is longer            */
  85.         SELECT count(*) INTO :worked_longer FROM emp
  86.           WHERE hiredate < :hired;
  87.  
  88.         /* Get number of people with a higher salary */
  89.         SELECT count(*) INTO :higher_sal FROM emp
  90.           WHERE sal > :salary;
  91.  
  92.         /* Get number of people in the same department */
  93.         SELECT count(*) INTO :total_in_dept FROM emp
  94.           WHERE deptno = :dept;
  95.         END;
  96.  
  97.         END-EXEC;
  98.         /* -------------------------------- */
  99.         /* ----- End the PL/SQL block ----- */
  100.         /* -------------------------------- */
  101.  
  102.         /* Properly terminate character strings *
  103.          * returned by ORACLE                   */
  104.     jobtype.arr[jobtype.len] = '\0';
  105.     hired.arr[hired.len] = '\0';
  106.  
  107.         /* Display all the information */
  108.     printf("\n%s's job is: %s\n", empname.arr, jobtype.arr);
  109.     printf("Hired on: %s\n", hired.arr);
  110.     printf("    %d people have served longer\n", worked_longer);
  111.     printf("Salary is: %d\n", salary);
  112.     printf("    %d people have a higher salary\n", higher_sal);
  113.     printf("Department number is: %d\n", dept);
  114.     printf("    %d people in the department\n", total_in_dept);
  115.     }  /* End of loop */
  116.  
  117. errprint:
  118.     /* We end up here if an error occurs */
  119.     EXEC SQL WHENEVER SQLERROR CONTINUE;
  120.     printf("\n\n>>>>> Error during execution:\n");
  121.     /* Print ORACLE error message and log off the database */
  122.     printf("%s\n",sqlca.sqlerrm.sqlerrmc);
  123.     EXEC SQL ROLLBACK RELEASE;
  124.     exit(1);
  125. }
  126.