home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a524 / 42.ddi / demo / samplec.lis < prev    next >
Encoding:
File List  |  1991-03-04  |  16.6 KB  |  423 lines

  1. ORACLE RDBMS V6.0.27.9.2, transaction processing option - Production
  2. PL/SQL V1.0.30.0.1 - Production/ORACLE Precompiler 1.3.15 Sat Dec 15 06:20:25 1990 Page 1
  3. Options: include=/usr/user1/c/lib ireclen=132 oreclen=132 rebind=no select_error=no sqlcheck=limited
  4.  
  5.  
  6.     1  #ifdef RCSID
  7.     2  static char *RCSid = 
  8.     3     "$Header: samplec.pc,v 6.0 88/06/16 19:25:58 rchoplin Exp $ samplec.pc Copyr (c) 1986 Oracle";
  9.     4  #endif /* RCSID */
  10.     5  
  11.     6  /*
  12.     7   * $Date: 88/06/16 19:25:58 $ $Revision: 6.0 $ samplec.pc Copyr (c) 1986 Oracle
  13.     8   */
  14.     9  
  15.    10  /* VOID sample
  16.    11  
  17.    12     sample  is a simple example program which adds new employee
  18.    13             records to the personnel data base.  Checking
  19.    14             is done to insure the integrity of the data base.
  20.    15             The employee numbers are automatically selected using
  21.    16             the current maximum employee number + 10.
  22.    17  
  23.    18             The program queries the user for data as follows:
  24.    19  
  25.    20             Enter employee name:
  26.    21             Enter employee job:
  27.    22             Enter employee salary:
  28.    23             Enter employee dept:
  29.    24  
  30.    25             The program terminates if EOF (end of file) or a null
  31.    26             string (<return> key) is entered when the employee name
  32.    27             is requested.
  33.    28  
  34.    29             If the record is successfully inserted, the following
  35.    30             is printed:
  36.    31  
  37.    32             ename added to department dname as employee # nnnnnn
  38.    33  
  39.    34             To build and run SAMPLE (assumes that SQLCA file is in "SQLCA.H"):
  40.    35  
  41.    36                 $ pcc iname=samplec.pc prog=xxx$sample userid=scott/tiger
  42.    37                 $ cc -O -c samplec.c
  43.    38                 $ cc -o samplec samplec.o -lsql -lhlic -lhli
  44.    39                 $ samplec
  45.    40  
  46.    41            where "xxx" is your logon id. E.g.:
  47.    42  
  48.    43                 $ pcc iname=samplec prog=oracle$sample userid=scott/tiger
  49.    44  
  50.    45            The idea here is to pick a unique name for the program, so that
  51.    46            anyone precompiling pgms into "scott/tiger" will have unique names
  52.    47            for their pgms (e.g., if I have a prog=sample userid=scott/tiger;
  53.    48            and you have a prog=sample userid=scott/tiger, then we have a
  54.    49            conflict...).
  55.    50  */
  56.    51  #include <stdio.h>
  57.    52  #include <ctype.h>
  58.    53  
  59.    54  EXEC SQL BEGIN DECLARE SECTION;
  60.    55  VARCHAR uid[20];
  61.    56                                         /* username                            */
  62.    57  VARCHAR pwd[20];
  63.    58                                         /* password                            */
  64.    59  
  65. ORACLE RDBMS V6.0.27.9.2, transaction processing option - Production
  66. PL/SQL V1.0.30.0.1 - Production/ORACLE Precompiler 1.3.15 Sat Dec 15 06:20:25 1990 Page 2
  67. Options: include=/usr/user1/c/lib ireclen=132 oreclen=132 rebind=no select_error=no sqlcheck=limited
  68.  
  69.  
  70.    60  int     empno;                         /* employee number                     */
  71.    61  VARCHAR ename[15];
  72.    62                                         /* employee name                       */
  73.    63  int     deptno;                        /* department number                   */
  74.    64  VARCHAR dname[15];
  75.    65                                         /* department name                     */
  76.    66  
  77.    67  VARCHAR job[15];
  78.    68                                         /* employee job                        */
  79.    69  int     sal;                           /* employee salary                     */
  80.    70  EXEC SQL END DECLARE SECTION;
  81.    71  EXEC SQL INCLUDE sqlca.h;
  82.     1  /*
  83.     2   * $Header: sqlca.h,v 10301.3 89/02/22 11:07:56 nsalah Exp $ sqlca.h Copyr (c) 1987 Oracle
  84.     3   */
  85.     4  
  86.     5  /* v1pcc,10301,v1pcc$src:[defs.10301] */
  87.     6  /* Copyright (c) 1985,1986 by Oracle Corporation. */
  88.     7   
  89.     8  /*
  90.     9  NAME
  91.    10    SQLCA : SQL Communications Area.
  92.    11  FUNCTION
  93.    12    Contains no code. Oracle fills in the SQLCA with status info
  94.    13    during the execution of a SQL stmt.
  95.    14  NOTES
  96.    15    If the symbol SQLCA_STORAGE_CLASS is defined, then the SQLCA
  97.    16    will be defined to have this storage class. For example:
  98.    17   
  99.    18      #define SQLCA_STORAGE_CLASS extern
  100.    19   
  101.    20    will define the SQLCA as an extern.
  102.    21   
  103.    22    If the symbol SQLCA_INIT is defined, then the SQLCA will be
  104.    23    statically initialized. Although this is not necessary in order
  105.    24    to use the SQLCA, it is a good pgming practice not to have
  106.    25    unitialized variables. However, some C compilers/OS's don't
  107.    26    allow automatic variables to be init'd in this manner. Therefore,
  108.    27    if you are INCLUDE'ing the SQLCA in a place where it would be
  109.    28    an automatic AND your C compiler/OS doesn't allow this style
  110.    29    of initialization, then SQLCA_INIT should be left undefined --
  111.    30    all others can define SQLCA_INIT if they wish.
  112.    31   
  113.    32  MODIFIED
  114.    33    Clare      12/06/84 - Ch SQLCA to not be an extern.
  115.    34    Clare      10/21/85 - Add initialization.
  116.    35    Bradbury   01/05/86 - Only initialize when SQLCA_INIT set
  117.    36    Clare      06/12/86 - Add SQLCA_STORAGE_CLASS option.
  118.    37  */
  119.    38   
  120.    39  #ifndef SQLCA
  121.    40  #define SQLCA 1
  122.    41   
  123.    42  struct   sqlca
  124.    43           {
  125.    44           /* ub1 */ char    sqlcaid[8];
  126.    45           /* b4  */ long    sqlabc;
  127.    46           /* b4  */ long    sqlcode;
  128.    47           struct
  129. ORACLE RDBMS V6.0.27.9.2, transaction processing option - Production
  130. PL/SQL V1.0.30.0.1 - Production/ORACLE Precompiler 1.3.15 Sat Dec 15 06:20:25 1990 Page 3
  131. Options: include=/usr/user1/c/lib ireclen=132 oreclen=132 rebind=no select_error=no sqlcheck=limited
  132.  
  133.  
  134.    48             {
  135.    49             /* ub2 */ unsigned short sqlerrml;
  136.    50             /* ub1 */ char           sqlerrmc[70];
  137.    51             } sqlerrm;
  138.    52           /* ub1 */ char    sqlerrp[8];
  139.    53           /* b4  */ long    sqlerrd[6];
  140.    54           /* ub1 */ char    sqlwarn[8];
  141.    55           /* ub1 */ char    sqlext[8];
  142.    56           };
  143.    57   
  144.    58  #ifdef   SQLCA_STORAGE_CLASS
  145.    59  SQLCA_STORAGE_CLASS struct sqlca sqlca
  146.    60  #else
  147.    61           struct sqlca sqlca
  148.    62  #endif
  149.    63   
  150.    64  #ifdef  SQLCA_INIT
  151.    65           = {
  152.    66           {'S', 'Q', 'L', 'C', 'A', ' ', ' ', ' '},
  153.    67           sizeof(struct sqlca),
  154.    68           0,
  155.    69           { 0, {0}},
  156.    70           {'N', 'O', 'T', ' ', 'S', 'E', 'T', ' '},
  157.    71           {0, 0, 0, 0, 0, 0},
  158.    72           {0, 0, 0, 0, 0, 0, 0, 0},
  159.    73           {0, 0, 0, 0, 0, 0, 0, 0}
  160.    74           }
  161.    75  #endif
  162.    76           ;
  163.    77   
  164.    78  #endif
  165.    79   
  166.    80  /* end SQLCA */
  167.    72  
  168.    73  main()
  169.    74  {
  170.    75  
  171.    76  /* -----------------------------------------------------------------------------
  172.    77     logon to ORACLE, and open the cursors. The program exits if any errors occur.
  173.    78  ----------------------------------------------------------------------------- */
  174.    79  
  175.    80     strcpy(uid.arr,"SCOTT");
  176.    81     uid.len = strlen(uid.arr);
  177.    82     strcpy(pwd.arr,"TIGER");
  178.    83     pwd.len = strlen(pwd.arr);
  179.    84  
  180.    85     EXEC SQL WHENEVER SQLERROR GOTO errexit;
  181.    86     EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
  182.    87  
  183.    88  /* -----------------------------------------------------------------------------
  184.    89     Retrieve the current maximum employee number
  185.    90  ----------------------------------------------------------------------------- */
  186.    91  
  187.    92     EXEC SQL SELECT NVL(MAX(EMPNO),0) + 10
  188.    93                INTO :empno
  189.    94                FROM EMP;
  190.    95  
  191.    96  /* -----------------------------------------------------------------------------
  192.    97     read the user's input from STDIN.  If the employee name is
  193. ORACLE RDBMS V6.0.27.9.2, transaction processing option - Production
  194. PL/SQL V1.0.30.0.1 - Production/ORACLE Precompiler 1.3.15 Sat Dec 15 06:20:25 1990 Page 4
  195. Options: include=/usr/user1/c/lib ireclen=132 oreclen=132 rebind=no select_error=no sqlcheck=limited
  196.  
  197.  
  198.    98     not entered, exit.
  199.    99     Verify that the entered department number is valid and echo the
  200.   100     department's name
  201.   101  ----------------------------------------------------------------------------- */
  202.   102  
  203.   103     for( ; ; empno+=10 )
  204.   104       {
  205.   105       int l;
  206.   106  
  207.   107       /* Get employee name to be inserted.
  208.   108  
  209.   109          IMPORTANT NOTE: beware of coding as follows (I got burnt, 1st time):
  210.   110  
  211.   111            ename.len = asks("Enter employee name  : ", ename.arr);
  212.   112            if ( ename.len <= 0 )
  213.   113              ..etc..
  214.   114  
  215.   115          In the above, asks() returns an int, but ename.len is an unsigned
  216.   116          short (see SQLCA). Therefore, the "if" fails for <EOF> (which returns
  217.   117          -1) because, by definition, the unsigned short can't be negative.
  218.   118       */
  219.   119       l = asks("Enter employee name  : ",ename.arr);
  220.   120  
  221.   121       if ( l <= 0 )
  222.   122         break;
  223.   123  
  224.   124       ename.len = l;
  225.   125  
  226.   126       job.len = asks("Enter employee job   : ",job.arr);
  227.   127       askn("Enter employee salary: ",&sal);
  228.   128       for ( ; ; )
  229.   129         {
  230.   130         if ( askn("Enter employee dept  :   ",&deptno) < 0 )
  231.   131           break;
  232.   132  
  233.   133         EXEC SQL WHENEVER NOT FOUND GOTO nodept;
  234.   134         EXEC SQL SELECT DNAME
  235.   135                    INTO :dname
  236.   136                    FROM DEPT
  237.   137                    WHERE DEPTNO = :deptno;
  238.   138  
  239.   139         dname.arr[dname.len] = '\0';
  240.   140  
  241.   141         EXEC SQL WHENEVER NOT FOUND STOP;
  242.   142  
  243.   143         /* Here if deptno was found in dbs. Insert new employee into dbs. */
  244.   144  
  245.   145         EXEC SQL INSERT INTO EMP(EMPNO,ENAME,JOB,SAL,DEPTNO)
  246.   146                    VALUES (:empno,:ename,:job,:sal,:deptno);
  247.   147  
  248.   148         printf("\n%s added to the %s department as employee number %d\n",
  249.   149           ename.arr,dname.arr,empno);
  250.   150         break;
  251.   151  
  252.   152         /* Here if deptno NOT found in dbs */
  253.   153         nodept:
  254.   154           printf("\nNo such department\n");
  255.   155           continue;
  256.   156         }
  257. ORACLE RDBMS V6.0.27.9.2, transaction processing option - Production
  258. PL/SQL V1.0.30.0.1 - Production/ORACLE Precompiler 1.3.15 Sat Dec 15 06:20:25 1990 Page 5
  259. Options: include=/usr/user1/c/lib ireclen=132 oreclen=132 rebind=no select_error=no sqlcheck=limited
  260.  
  261.  
  262.   157       }
  263.   158  
  264.   159  /* -----------------------------------------------------------------------------
  265.   160     close the cursors and log off from ORACLE
  266.   161  ----------------------------------------------------------------------------- */
  267.   162  
  268.   163     EXEC SQL COMMIT WORK RELEASE;
  269.   164     printf ("\nEnd of the C/ORACLE example program.\n");
  270.   165     return(1);
  271.   166  
  272.   167  errexit:
  273.   168     errrpt();
  274.   169     EXEC SQL WHENEVER SQLERROR CONTINUE;
  275.   170     EXEC SQL ROLLBACK WORK RELEASE;
  276.   171     return(0);
  277.   172  }
  278.   173  
  279.   174  /*------------------------------------------------------------------------------
  280.   175  COUNT askn(text,variable)
  281.   176  
  282.   177     print the 'text' on STDOUT and read an integer variable from
  283.   178     SDTIN.
  284.   179  
  285.   180     text points to the null terminated string to be printed
  286.   181     variable points to an integer variable
  287.   182  
  288.   183     askn returns a 1 if the variable was read successfully or a
  289.   184         -1 if -eof- was encountered
  290.   185  ----------------------------------------------------------------------------- */
  291.   186  
  292.   187  int askn(text,variable)
  293.   188     char text[];
  294.   189     int  *variable;
  295.   190     {
  296.   191     char s[20];
  297.   192     printf(text);
  298.   193     if ( gets(s) == (char *)0 )
  299.   194       return(EOF);
  300.   195  
  301.   196     *variable = atoi(s);
  302.   197     return(1);
  303.   198     }
  304.   199  
  305.   200  /* -----------------------------------------------------------------------------
  306.   201  COUNT asks(text,variable)
  307.   202  
  308.   203     print the 'text' on STDOUT and read up to 'len' characters into
  309.   204     the buffer pointed to by variable from STDIN.
  310.   205  
  311.   206     text points to the null terminated string to be printed
  312.   207     variable points to a buffer of at least 'len'+1 characters
  313.   208  
  314.   209     asks returns the number of character read into the string, or a
  315.   210         -1 if -eof- was encountered
  316.   211  ----------------------------------------------------------------------------- */
  317.   212  
  318.   213  int asks(text,variable)
  319.   214     char text[],variable[];
  320.   215     {
  321. ORACLE RDBMS V6.0.27.9.2, transaction processing option - Production
  322. PL/SQL V1.0.30.0.1 - Production/ORACLE Precompiler 1.3.15 Sat Dec 15 06:20:25 1990 Page 6
  323. Options: include=/usr/user1/c/lib ireclen=132 oreclen=132 rebind=no select_error=no sqlcheck=limited
  324.  
  325.  
  326.   216     printf(text);
  327.   217     return( gets(variable) == (char *)0 ? EOF : strlen(variable) );
  328.   218     }
  329.   219  
  330.   220  /* -----------------------------------------------------------------------------
  331.   221  VOID errrpt()
  332.   222  
  333.   223     errrpt prints the ORACLE error msg and number.
  334.   224  ----------------------------------------------------------------------------- */
  335.   225  
  336.   226  errrpt()
  337.   227     {
  338.   228     printf("\n%.70s (%d)\n", sqlca.sqlerrm.sqlerrmc, -sqlca.sqlcode);
  339.   229     return(0);
  340.   230     }
  341. ORACLE RDBMS V6.0.27.9.2, transaction processing option - Production
  342. PL/SQL V1.0.30.0.1 - Production/ORACLE Precompiler 1.3.15 Sat Dec 15 06:20:25 1990 Page 7
  343. Options: include=/usr/user1/c/lib ireclen=132 oreclen=132 rebind=no select_error=no sqlcheck=limited
  344.  
  345.  
  346. Host Variables
  347. --------------
  348.  
  349.    deptno                               Long Integer       Def: Line  63 in samplec.pc
  350.                                                            Ref: Line 134 in samplec.pc
  351.                                                            Ref: Line 145 in samplec.pc
  352.    dname                                Varchar(15)        Def: Line  64 in samplec.pc
  353.                                                            Ref: Line 134 in samplec.pc
  354.    empno                                Long Integer       Def: Line  60 in samplec.pc
  355.                                                            Ref: Line  92 in samplec.pc
  356.                                                            Ref: Line 145 in samplec.pc
  357.    ename                                Varchar(15)        Def: Line  61 in samplec.pc
  358.                                                            Ref: Line 145 in samplec.pc
  359.    job                                  Varchar(15)        Def: Line  67 in samplec.pc
  360.                                                            Ref: Line 145 in samplec.pc
  361.    pwd                                  Varchar(20)        Def: Line  57 in samplec.pc
  362.                                                            Ref: Line  86 in samplec.pc
  363.    sal                                  Long Integer       Def: Line  69 in samplec.pc
  364.                                                            Ref: Line 145 in samplec.pc
  365.    uid                                  Varchar(20)        Def: Line  55 in samplec.pc
  366.                                                            Ref: Line  86 in samplec.pc
  367. ORACLE RDBMS V6.0.27.9.2, transaction processing option - Production
  368. PL/SQL V1.0.30.0.1 - Production/ORACLE Precompiler 1.3.15 Sat Dec 15 06:20:25 1990 Page 8
  369. Options: include=/usr/user1/c/lib ireclen=132 oreclen=132 rebind=no select_error=no sqlcheck=limited
  370.  
  371.  
  372. SQL Identifiers
  373. ---------------
  374.  
  375. UNIX                 DEFINE                                Pre-defined symbol
  376. ORACLE RDBMS V6.0.27.9.2, transaction processing option - Production
  377. PL/SQL V1.0.30.0.1 - Production/ORACLE Precompiler 1.3.15 Sat Dec 15 06:20:25 1990 Page 9
  378. Options: include=/usr/user1/c/lib ireclen=132 oreclen=132 rebind=no select_error=no sqlcheck=limited
  379.  
  380.  
  381. Current Options
  382. ---------------
  383.  
  384. areasize       16384
  385. asacc          no
  386. errors         yes
  387. hold_cursor    no
  388. host           C
  389. iname          samplec.pc
  390. inline         yes
  391. ireclen        132
  392. lname          samplec.lis
  393. lreclen        132
  394. ltype          long
  395. maxopencursors 10
  396. mode           oracle
  397. oname          samplec.c
  398. oreclen        132
  399. pagelen        66
  400. program        SAMPLEC
  401. release_cursor no
  402. sqlcheck       limited
  403. xref           yes
  404. ORACLE RDBMS V6.0.27.9.2, transaction processing option - Production
  405. PL/SQL V1.0.30.0.1 - Production/ORACLE Precompiler 1.3.15 Sat Dec 15 06:20:25 1990 Page 10
  406. Options: include=/usr/user1/c/lib ireclen=132 oreclen=132 rebind=no select_error=no sqlcheck=limited
  407.  
  408.  
  409. Statistics
  410. ----------
  411.  
  412. Return code: 0
  413. Number of messages at severity I: 0
  414. Number of messages at severity W: 0
  415. Number of messages at severity E: 0
  416. Number of messages at severity S: 0
  417. Number of messages at severity U: 0
  418. Number of input lines: 310
  419. Number of host variables declared: 8
  420. Number of cursor names: 0
  421. Number of statement names: 0
  422. Maximum bytes used: 5688
  423.