       identification division.program-id. list.
       
       data division.
       working-storage section.
       EXEC SQL INCLUDE sqlca END-EXEC.
       EXEC SQL BEGIN DECLARE SECTION END-EXEC.
       EXEC SQL INCLUDE 'customer2.cob' END-EXEC.
       EXEC SQL INCLUDE 'order2.cob' END-EXEC.
       01 con pic x(255).
       01 usr pic x(255).
       01 pwd pic x(255).
       01 sumval pic s9(8)v9(2) comp-3.
       EXEC SQL END DECLARE SECTION END-EXEC.
       01 last-customer-id pic s9(8) comp.
       01 name-len pic s9(8) comp.
       01 phone-len pic s9(8) comp.
       01 source-len pic s9(8) comp.
       01 address1-len pic s9(8) comp.
       01 address2-len pic s9(8) comp.
       01 status-len pic s9(8) comp.
       01 len pic s9(8) comp.
       01 order-id-2 pic 9(8) display.
       01 sumval2 pic 9(8)v9(2) display.
       EXEC SQL
          DECLARE custpot CURSOR FOR
          SELECT customer.id AS cid, name, phone, source
          FROM customer JOIN customer_potential
                        ON customer.id = customer_potential.id
          ORDER BY cid
       END-EXEC.
       EXEC SQL
          DECLARE custact CURSOR FOR
          SELECT customer.id AS cid,name,address1,address2,phone,
                 order_main.id AS oid,status,
                 SUM(quantity*price) AS sumval
          FROM customer JOIN customer_actual
                             ON customer.id = customer_actual.id
                        JOIN order_main
                             ON customer.id = order_main.customer
                        JOIN order_line
                             ON order_main.id = order_line.order_main
                        GROUP BY customer.id,
                                 name,
                                 address1,
                                 address2,
                                 phone,
                                 order_main.id,
                                 status
                        ORDER BY cid, oid
       END-EXEC.
       
       procedure division.
       main-paragraph.
           move "test" to con
           move "SYSADM" to usr
           move "hemmeligt" to pwd
           EXEC SQL CONNECT TO :con USER :usr USING :pwd END-EXEC
           EXEC SQL OPEN custpot END-EXEC
           move 0 to sqlcode
           perform until not sqlcode = 0
               EXEC SQL
                   FETCH custpot
                   INTO :customer-id,
                        :customer-name,
                        :customer-phone,
                        :customer-potential-source
               END-EXEC
               if sqlcode = 0 then
                   perform display-potential-paragraph
               end-if
           end-perform
           EXEC SQL CLOSE custpot END-EXEC
           EXEC SQL OPEN custact END-EXEC
           move 0 to sqlcode
           move 0 to last-customer-id
           perform until not sqlcode = 0
               EXEC SQL
                   FETCH custact
                   INTO :customer-id,
                        :customer-name,
                        :customer-actual-address1,
                        :customer-actual-address2,
                        :customer-phone,
                        :order-id,
                        :order-status,
                        :sumval
               END-EXEC
               if sqlcode = 0 then
                   if customer-id not = last-customer-id then
                       perform display-actual-paragraph
                       move customer-id to last-customer-id
                   end-if
                   perform display-order-paragraph
               end-if
           end-perform
           EXEC SQL CLOSE custact END-EXEC
           stop run.
       display-potential-paragraph.
           move 32 to len
           call "trim"
               using customer-name, len
               giving name-len
           end-call
           move 16 to len
           call "trim"
               using customer-phone, len
               giving phone-len
           end-call
           move 256 to len
           call "trim"
               using customer-potential-source, len
               giving source-len
           end-call
           display "**future customer** " customer-name(1:name-len)
                   ", " customer-phone(1:phone-len)
                   ", " customer-potential-source(1:source-len).
       display-actual-paragraph.
           move 32 to len
           call "trim"
               using customer-name, len
               giving name-len
           end-call
           move 64 to len
           call "trim"
               using customer-actual-address1, len
               giving address1-len
           end-call
           move 64 to len
           call "trim"
               using customer-actual-address2, len
               giving address2-len
           end-call
           move 16 to len
           call "trim"
               using customer-phone, len
               giving phone-len
           end-call
           display customer-name(1:name-len)
                   ", " customer-actual-address1(1:address1-len)
                   ", " customer-actual-address2(1:address2-len)
                   ", " customer-phone(1:phone-len).
       display-order-paragraph.
           move order-id to order-id-2
           move 16 to len
           call "trim"
               using order-status, len
               giving status-len
           end-call
           move sumval to sumval2
           display "  " order-id-2
                   ", " order-status(1:status-len)
                   ", ", sumval2.
