www.sas.com > Service and Support > Technical Support
 
Technical Support SAS - The power to know(tm)
  TS Home | Intro to Services | News and Info | Contact TS | Site Map | FAQ | Feedback


/*-------------------------------------------------------------------+
|                COPYRIGHT (C) 1995, SAS INSTITUTE INC.              |
|                  UNPUBLISHED - ALL RIGHTS RESERVED                 |
|                     S A S / C   S A M P L E                        |
|                                                                    |
|                                                                    |
|         NAME: ORASASC                                              |
|     LANGUAGE: C                                                    |
|      PURPOSE: This program is an example of using the ORACLE Pro*C |
|               Precompiler with SAS/C.                              |
|   MVS -                                                            |
|      COMPILE: See JCL in prefix.SAMPLE.AUX(SQLJCL).                |
|         LINK: See JCL in prefix.SAMPLE.AUX(SQLJCL).                |
|      EXECUTE: See JCL in prefix.SAMPLE.AUX(ORALOAD).               |
|         NOTE: This example requires that you have the ORACLE Pro*C |
|               Precompiler, version 1.3.                            |
+-------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXEMPLOY 600
#define SASC
#define SQLCA_INIT
void printdb();
void sortdb();
void getdb();
static int cmp(const void *,const void *);

EXEC SQL BEGIN DECLARE SECTION;     /* Declare host variables */
  char *uname = "SCOTT";
  char *pw = "TIGER";
 int empno;                     /* temporary variables for SQL */
 VARCHAR elast[10];
 char efirst[2];
 VARCHAR j[4];
 int d;
 float s;
 char sqlstmt[110];         /* dynamic SQL statement */
EXEC SQL END DECLARE SECTION;

  typedef struct {             /* Employee Structure */
   int empno;                  /* employee number,   */
   char efirst[2];             /* first initial,     */
   char elast[10];          /* last name,         */
   char job[4];             /* position abbrev.   */
   int deptno;                 /* dept number        */
   float sal;                  /* monthly salary     */
  } EMP_STRUCT;
 EMP_STRUCT e[MAXEMPLOY];

 char temp[5];
 char c;
 int i,count;             /* array counter,record counter */

EXEC SQL DECLARE SQLOBJ STATEMENT;

EXEC SQL INCLUDE SQLCAC;     /* this was sqlca teb*/

void main()
{
  EXEC SQL WHENEVER SQLERROR GOTO iferror;
  EXEC SQL CONNECT :uname IDENTIFIED BY :pw;
   printf("Connected to ORACLE. \n");

  for(i=0;i<110;i++)              /* initialize sqlstmt */
   sqlstmt[i] = ' ';
                                  /* set sqlstmt for dynamic call */
  strcpy(sqlstmt,"INSERT INTO EMP (EMPNO, EFIRST, ELAST, JOB, SAL,");
  strcat(sqlstmt," DEPTNO) VALUES (:empno, :efirst, :elast, :j,");
  strcat(sqlstmt," :s, :d)");
  for(i=110;sqlstmt[i]='\0';i--)
    sqlstmt[i] = ' ';
  EXEC SQL PREPARE SQLOBJ FROM :sqlstmt;

 for(;;)
 {
 /* get the current database from ORACLE */
  getdb();

 /* sort the database by name */
  sortdb();

 /* print the database */
  printdb();

  printf("Would you like to 1) insert, or 2) delete a record, or ");
  printf("0) quit? \n");
  while((c = getchar()) !=  0 )
   {
    getchar();              /* get rid of excess characters */
    switch (c)
    {
    case '0':              /* QUIT */
     {
      EXEC SQL COMMIT WORK RELEASE;
      exit(0);
     }
     case '1':             /* INSERT */
      {
      strcpy(j.arr,"New");   /* set initial values of 3 vars */
      s = 0.0;
      d = 100;
      printf("Enter the employee information in the ");
      printf("form: LASTNAME FIRSTINITIAL EMPNO \n");
      printf("The lastname should be <10 chars. \n");
      scanf("%s %s %d",elast.arr,efirst,&empno);
      getchar();            /* get rid of excess characters */

     EXEC SQL EXECUTE SQLOBJ USING :empno,:efirst,:elast,:j,:s,:d;

      break;
      }
     case '2':          /* DELETE */
     {
      printf("Enter the Employee Number to delete: ");
      scanf("%d",&empno);
      getchar();            /* get rid of excess characters */

      EXEC SQL DELETE FROM EMP WHERE EMPNO = :empno;
      break;
     }
    default:
     {
      printf("Invalid response. Please choose again. \n");
      break;
     }
    }
   getdb();         /* updated database -- sort and print */
   sortdb();
   printdb();

  printf("Would you like to 1) insert, or 2) delete a record, or ");
  printf("0) quit? \n");
  }
 }

 /* if an SQL error should result during processing */
 iferror:
 {
  printf("SQL error: %.70s \n",sqlca.sqlerrm.sqlerrmc);
  EXEC SQL WHENEVER SQLERROR CONTINUE;
  EXEC SQL ROLLBACK WORK RELEASE;
  exit(1);
 }

  return;
}
void printdb()
/*-----------------------------------------------------------------*/
/* This routine prints the current database as read in from ORACLE */
/* by the routine getdb(). The information is stored in e array.   */
/*-----------------------------------------------------------------*/
{
 /* print the database */
  printf("The current database: \n");
  printf("Employee  Emp No.  Position   Salary   DeptNo.\n");
 for(i=0;ielast,esecond->elast);
  if(x) return(x);
  else return(strcmp(efirst->efirst,esecond->efirst));
}

Copyright (c) 2000 SAS Institute Inc. All Rights Reserved.
Terms of Use & Legal Information | Privacy Statement