home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l360 / 3.ddi / SQLDEMO.@EM / SQLDEMO.CBL < prev    next >
Encoding:
Text File  |  1991-05-10  |  11.6 KB  |  322 lines

  1.       $set ans85 mf noosvs
  2.       ************************************************************
  3.       *                                                          *
  4.       *              (C) Micro Focus Ltd. 1989,1991              *
  5.       *                                                          *
  6.       *                     SQLDEMO.CBL                          *
  7.       *                                                          *
  8.       *    This program demonstrates the use of SQL from within  *
  9.       *    a COBOL program.                                      *
  10.       *                                                          *
  11.       *    The program SQLPREP must be compiled and run before   *
  12.       *    this program is compiled to allow successful          *
  13.       *    compilation.                                          *
  14.       *                                                          *
  15.       ************************************************************
  16.        working-storage section.
  17.        78 no-data            value 100.
  18.       *SQLCODE for no data available
  19.        01 y-or-n             pic x.
  20.        01 display-line.
  21.            03 disp-id        pic z(5).
  22.            03 filler         pic x.
  23.            03 disp-name      pic x(9).
  24.            03 filler         pic x.
  25.            03 disp-dept      pic z(5).
  26.            03 filler         pic x.
  27.            03 disp-job       pic x(5).
  28.            03 filler         pic x.
  29.            03 disp-years     pic z(5).
  30.            03 yrs-nul-val redefines disp-years pic x(5).
  31.            03 filler         pic x.
  32.            03 disp-salary    pic z(7).9(2).
  33.            03 filler         pic x.
  34.            03 disp-comm      pic z(7).9(2).
  35.            03 com-nul-val redefines disp-comm pic x(10).
  36.        01 disp-n60         pic zz9.9.
  37.  
  38.       * An SQLCA is needed to communicate with database manager
  39.        exec sql include sqlca end-exec
  40.  
  41.       * Host variables for database interrogation
  42.        exec sql begin declare section end-exec
  43.        01 wsid             pic s9(4) packed-decimal.
  44.       * You may use comp-3, comp-5 or packed-decimal for host variables
  45.        01 nme              pic x(9).
  46.        01 dept             pic s9(4) packed-decimal.
  47.        01 job              pic x(5).
  48.        01 years            pic s9(4) packed-decimal.
  49.        01 salary           pic s9(5)v9(2) packed-decimal.
  50.        01 comm             pic s9(5)v9(2) packed-decimal.
  51.        01 location         pic x(13).
  52.        01 deptname         pic x(14).
  53.        01 car              pic x(20).
  54.        01 n60              pic s9(3)v9 packed-decimal.
  55.        01 avalue           pic s9(4) packed-decimal.
  56.       * Now two indicator variables are needed because years and comm
  57.       * may have null values. Indicator variables must be comp-5.
  58.        01 yrsnul           pic s9(4) comp-5.
  59.        01 commnul          pic s9(4) comp-5.
  60.       * Now the base string for the prepare example
  61.        01 prep             pic x(34).
  62.        exec sql end declare section end-exec
  63.  
  64.        procedure division.
  65.            perform sub-select
  66.            perform select-with-cursor
  67.            perform full-select
  68.            perform view-example
  69.            perform insert-example
  70.            perform prepare-example
  71.            stop run.
  72.  
  73.        sub-select.
  74.       * This example is a straight forward select statement
  75.       * Note the use of indicator variables yrsnul and commnul they
  76.       * are negative if the relevant value from the database is null
  77.       *
  78.            display
  79.               "This demo will select from table STAFF of the sample SQL"
  80.            display
  81.              "database. The selection will be based on the value of the"
  82.            display
  83.            "column 'ID', the entry with ID equal to the value you enter"
  84.            display
  85.                "will be displayed"
  86.            display "Enter value (table values go from 10 - 350)"
  87.            accept avalue
  88.  
  89.            exec sql
  90.                select id, name, dept, job, years, salary, comm
  91.                    into :wsid, :nme, :dept, :job, :years:yrsnul,
  92.                         :salary, :comm:commnul
  93.                    from staff
  94.                    where id = :avalue
  95.            end-exec
  96.  
  97.            if sqlcode = zero
  98.                perform make-line
  99.                display display-line
  100.            else
  101.                if sqlcode = no-data
  102.                    display "No row with that ID"
  103.                else
  104.                   perform sql-err
  105.                end-if
  106.            end-if.
  107.  
  108.        select-with-cursor.
  109.            display spaces
  110.            display
  111.               "This demo will select from table STAFF of the sample SQL"
  112.            display
  113.              "database. The selection will be based on the value of the"
  114.            display
  115.             "column ID, all entries with a value greater than the value"
  116.            display "you enter will be displayed."
  117.            display "Enter cutoff value (table values go from 10 - 350)"
  118.            accept avalue
  119.  
  120.       * Must use a cursor as many values are expected
  121.            exec sql
  122.                declare c1 cursor for
  123.                select id, name, dept, job, years, salary, comm
  124.                    from staff
  125.                    where id > :avalue
  126.            end-exec
  127.  
  128.       * Open the cursor to process the database entries
  129.            exec sql
  130.                open c1
  131.            end-exec
  132.  
  133.            perform until sqlcode not = zero
  134.       * SQLCODE will be zero as long as it has successfully fetched data
  135.                exec sql
  136.                    fetch c1 into :wsid , :nme, :dept, :job,
  137.                                  :years:yrsnul, :salary, :comm:commnul
  138.                end-exec
  139.                if sqlcode = zero
  140.                    perform make-line
  141.                    display display-line
  142.                end-if
  143.            end-perform.
  144.  
  145.        full-select.
  146.       * This example uses a cursor to handle the data extracted by two
  147.       * select statements joined by an intersect statement, other set
  148.       * operations may be substituted
  149.       *
  150.            display spaces
  151.            display
  152.               "This demo shows the usage of intersect across two tables"
  153.            display
  154.              "in the same database, the data extracted is the DEPT from"
  155.            display "STAFF and the DEPTNUMB from ORG"
  156.            perform wait-accept
  157.  
  158.            exec sql
  159.                declare c2 cursor for
  160.                select dept from staff
  161.                intersect
  162.                select deptnumb from org
  163.            end-exec
  164.  
  165.            exec sql
  166.                open c2
  167.            end-exec
  168.  
  169.            perform until sqlcode not = zero
  170.                exec sql
  171.                    fetch c2 into :dept
  172.                end-exec
  173.                if sqlcode = zero
  174.                    move dept to disp-dept
  175.                    display disp-dept
  176.                end-if
  177.            end-perform.
  178.  
  179.        view-example.
  180.       * This example uses the view PEOPLE_LOC created by DEMO1
  181.            display spaces
  182.            display
  183.               "This demo will create a view over the two tables ORG and"
  184.            display
  185.             "STAFF then will extract all data from the view. The result"
  186.            display
  187.                "of the view is a list of all employees (from STAFF) and"
  188.            display "their place of work (from ORG)"
  189.            perform wait-accept
  190.  
  191.       * Once the view is created it may be treated just like a table
  192.            exec sql
  193.                declare c3 cursor for
  194.                    select name,location from people_loc
  195.            end-exec
  196.  
  197.            exec sql
  198.                open c3
  199.            end-exec
  200.  
  201.            perform until sqlcode not = zero
  202.                exec sql
  203.                    fetch c3 into :nme,:location
  204.                end-exec
  205.                if sqlcode = zero
  206.                    display nme" "location
  207.                end-if
  208.            end-perform.
  209.  
  210.        insert-example.
  211.       * This example inserts a row into MF_TABLE which is created by
  212.       * SQLPREP. The row is then queried and deleted to prevent any
  213.       * problems which could be caused if the program was run a second
  214.       * time with identical rows in the table. The select would then
  215.       * fail as the resultant data would comprise more than one row
  216.       * which would require a cursor. Note the use of apostrophe (')
  217.       * instead of quotes (") to delimit the SQL character data.
  218.            display spaces
  219.            display
  220.              "This demo will insert a row into the table MF_TABLE which"
  221.            display
  222.            "is created by DEMO1 and then will query the row. The values"
  223.            display "inserted are: Roger, Ferrari 328 GTB, 6.4"
  224.            perform wait-accept
  225.  
  226.            exec sql
  227.                insert into mf_table (name, car, nto60)
  228.                    values ('Roger','Ferrari 328 GTB',6.4)
  229.            end-exec
  230.  
  231.            exec sql
  232.                select name,car,nto60
  233.                    into :nme,:car,:n60
  234.                    from mf_table
  235.                    where name='Roger'
  236.            end-exec
  237.  
  238.            if sqlcode = zero
  239.                move n60 to disp-n60
  240.                display nme" "car" "disp-n60
  241.            else
  242.                perform sql-err
  243.            end-if
  244.  
  245.       * Now to delete the row
  246.            exec sql
  247.                delete from mf_table
  248.                    where name='Roger'
  249.            end-exec.
  250.  
  251.        prepare-example.
  252.       * This example inserts data into MF-TABLE (created by SQLPREP)
  253.       * by use of the SQL PREPARE and EXECUTE statements. Note the use
  254.       * of the parameter markers '?' which are replaced by the actual
  255.       * data during the EXECUTE statement
  256.       *
  257.            display spaces
  258.            display
  259.            "This example inserts one row into MF_TABLE using a prepared"
  260.            display
  261.               "SQL statement, then reads it back. The row inserted is:-"
  262.            display "Elaine, Lamborghini, 4.9"
  263.            perform wait-accept
  264.            move "insert into mf_table values(?,?,?)" to prep
  265.            exec sql
  266.                prepare prep_stat from :prep
  267.            end-exec
  268.            if sqlcode not = zero
  269.                perform sql-err
  270.            else
  271.                move "Elaine" to nme
  272.                move "Lamborghini" to car
  273.                move 4.9 to n60
  274.                exec sql
  275.                    execute prep_stat using :nme, :car, :n60
  276.                end-exec
  277.            end-if
  278.  
  279.            exec sql
  280.                select name,car,nto60
  281.                    into :nme, :car, :n60
  282.                    from mf_table
  283.                    where name='Elaine'
  284.            end-exec
  285.            if sqlcode = zero
  286.                move n60 to disp-n60
  287.                display nme" "car" "disp-n60
  288.            else
  289.                perform sql-err
  290.            end-if
  291.       * Now to delete row
  292.            exec sql
  293.                delete from mf_table
  294.                    where name='Elaine'
  295.            end-exec.
  296.  
  297.        sql-err.
  298.            display "SQL error SQLCODE="sqlcode.
  299.  
  300.        make-line.
  301.            move spaces to display-line
  302.            move wsid to disp-id
  303.            move nme to disp-name
  304.            move dept to disp-dept
  305.            move job to disp-job
  306.            move salary to disp-salary
  307.       * Now check for null values and handle accordingly
  308.            if yrsnul < 0
  309.                move "NULL" to yrs-nul-val
  310.            else
  311.                move years to disp-years
  312.            end-if
  313.            if commnul < 0
  314.                move "NULL" to com-nul-val
  315.            else
  316.                move comm to disp-comm
  317.            end-if.
  318.  
  319.        wait-accept.
  320.            display "Press return to run demo"
  321.            accept y-or-n.
  322.