home *** CD-ROM | disk | FTP | other *** search
- #ifdef RCSID
- static char *RCSid =
- "$Header: examp6.pc,v 1002100.2 90/01/05 17:59:50 nsalah Exp $ examp6.pc Copyr (c) 1989 Oracle";
- #endif /* RCSID */
-
- /* V6PLS10021,DISK$DEV9:[PLS.DEMO.10021] */
- /************************************************************************
- * *
- * EMBEDDED PL/SQL DEMO *
- * *
- * This program prompts the user for the name of an employee. It then *
- * executes a PL/SQL block which contains four SELECT statements in *
- * order to obtain information on this employee. The information *
- * returned includes: employee's job, the date the employee was *
- * hired, the number of people who have served the company longer, *
- * the employee's salary, the number of people in the company who have *
- * a higher salary, the employee's department number, and the total *
- * number of people in that department. *
- * *
- * This program is intended to show the use of host variables in *
- * PL/SQL. *
- * *
- * Copyright (c) 1989 by Oracle Corporation. *
- ************************************************************************/
-
- #include <stdio.h>
-
- EXEC SQL BEGIN DECLARE SECTION;
- varchar empname[11];
- varchar jobtype[9];
- varchar hired[9];
- int salary;
- int dept;
- int worked_longer;
- int higher_sal;
- int total_in_dept;
- varchar uid[20];
- varchar pwd[20];
- EXEC SQL END DECLARE SECTION;
-
- EXEC SQL INCLUDE SQLCA;
-
- main()
- {
-
- /* Set up userid and password */
- 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;
-
- /* Connect to ORACLE */
- EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
- printf(" connected.\n");
-
-
- for (;;) /* Loop infinitely */
- {
- printf("\n** Name of employee? (<return> to end) ");
- gets(empname.arr); /* Get the name */
- if (strlen(empname.arr) == 0) /* No name entered, */
- {
- EXEC SQL COMMIT WORK RELEASE; /* so log off ORACLE */
- exit(0); /* and exit program */
- }
- empname.len = strlen(empname.arr);
-
- /* ---------------------------------- */
- /* ----- Begin the PL/SQL block ----- */
- /* ---------------------------------- */
- EXEC SQL EXECUTE
-
- BEGIN
- SELECT job, hiredate, sal, deptno INTO
- :jobtype, :hired, :salary, :dept FROM emp
- WHERE ename = UPPER(:empname);
-
- /* Get number of people whose length of *
- * service is longer */
- SELECT count(*) INTO :worked_longer FROM emp
- WHERE hiredate < :hired;
-
- /* Get number of people with a higher salary */
- SELECT count(*) INTO :higher_sal FROM emp
- WHERE sal > :salary;
-
- /* Get number of people in the same department */
- SELECT count(*) INTO :total_in_dept FROM emp
- WHERE deptno = :dept;
- END;
-
- END-EXEC;
- /* -------------------------------- */
- /* ----- End the PL/SQL block ----- */
- /* -------------------------------- */
-
- /* Properly terminate character strings *
- * returned by ORACLE */
- jobtype.arr[jobtype.len] = '\0';
- hired.arr[hired.len] = '\0';
-
- /* Display all the information */
- printf("\n%s's job is: %s\n", empname.arr, jobtype.arr);
- printf("Hired on: %s\n", hired.arr);
- printf(" %d people have served longer\n", worked_longer);
- printf("Salary is: %d\n", salary);
- printf(" %d people have a higher salary\n", higher_sal);
- printf("Department number is: %d\n", dept);
- printf(" %d people in the department\n", total_in_dept);
- } /* End of loop */
-
- errprint:
- /* We end up here if an error occurs */
- EXEC SQL WHENEVER SQLERROR CONTINUE;
- printf("\n\n>>>>> Error during execution:\n");
- /* Print ORACLE error message and log off the database */
- printf("%s\n",sqlca.sqlerrm.sqlerrmc);
- EXEC SQL ROLLBACK RELEASE;
- exit(1);
- }
-