Publis/Subscribe function in COBOL
>> Embeded Delete
1. Use table SOS.ECUI.ORDERS.
INSERT INTO ORDERS(ORDER_NBR,AMOUNT) VALUES(1,1111);
INSERT INTO ORDERS(ORDER_NBR,AMOUNT) VALUES(2,2222);
INSERT INTO ORDERS(ORDER_NBR,AMOUNT) VALUES(3,3333);
2. In COBOL program
This program will declare a cursor, and then disply it. if all the records processed, it will wait for the new added record, until 1minute.
TEST-PB.
PERFORM BEGIN-TRAN THRU BEGIN-TRAN-EXIT.
MOVE 0 TO WS-AMOUNT.
MOVE SPACES TO WS-ORDER-NBR.
EXEC SQL
DECLARE GET_ORDERS CURSOR WITH HOLD FOR
SELECT ORDER_NBR, AMOUNT FROM
(DELETE FROM STREAM(SOS.ECUI.ORDERS)
FOR SKIP CONFLICT ACCESS) AS ORDERS
END-EXEC.
EXEC SQL
OPEN GET_ORDERS
END-EXEC.
ACCEPT WS-START FROM TIME.
MOVE 0 TO WS-TIME.
PERFORM PROCESS-ORDERS THRU PROCESS-ORDERS-EXIT
UNTIL WS-TIME >10000.
EXEC SQL
CLOSE GET_ORDERS
END-EXEC.
PERFORM END-TRAN THRU END-TRAN-EXIT.
TEST-PB-EXIT.
EXIT.
/
PROCESS-ORDERS.
ACCEPT WS-END FROM TIME.
COMPUTE WS-TIME = WS-END - WS-START.
IF WS-TIME >1000
GO TO PROCESS-ORDERS-EXIT.
EXEC SQL
FETCH GET_ORDERS INTO :WS-ORDER-NBR,:WS-AMOUNT
END-EXEC.
DISPLAY WS-ORDER-NBR, WS-AMOUNT.
PROCESS-ORDERS-EXIT.
EXIT.
3. Compile it and exec it
$DATA01.ECSRC 138> RUN ECTEXE
00000000010000001111HAS BEEN PROCESSED
00000000020000002222HAS BEEN PROCESSED
00000000030000003333HAS BEEN PROCESSED
4. Insert more records into table
>>INSERT INTO ORDERS(ORDER_NBR,AMOUNT) VALUES(4,4444);
---1 row(s) inserted.
>>INSERT INTO ORDERS(ORDER_NBR,AMOUNT) VALUES(5,5555);
---1 row(s) inserted.
>>INSERT INTO ORDERS(ORDER_NBR,AMOUNT) VALUES(6,6666);
---1 row(s) inserted.
5. In the program session, the new added records can be processed.
$DATA01.ECSRC 138> RUN ECTEXE
00000000010000001111HAS BEEN PROCESSED
00000000020000002222HAS BEEN PROCESSED
00000000030000003333HAS BEEN PROCESSED
00000000040000004444HAS BEEN PROCESSED
00000000050000005555HAS BEEN PROCESSED
00000000060000006666HAS BEEN PROCESSED
6. check the table
>>SELECT * FROM SOS.ECUI.ORDERS;
---0 row(s) selected.
>> Embeded Update
1. Table ORDERS, STATUS is "N"
>>SELECT * FROM SOS.ECUI.ORDERS;
ORDER_NBR AMOUNT STATUS
----------- ----------- ------
1 1111 N
2 2222 N
3 3333 N
---3 row(s) selected.
2. In COBOL Program
TEST-PB-UPDATE.
PERFORM BEGIN-TRAN THRU BEGIN-TRAN-EXIT.
MOVE 0 TO WS-AMOUNT, WS-ORDER-NBR.
EXEC SQL
DECLARE GET_ORDERS_UPD CURSOR WITH HOLD FOR
SELECT ORDER_NBR, AMOUNT FROM
(UPDATE STREAM(SOS.ECUI.ORDERS) SET STATUS =‘Y‘
WHERE STATUS =‘N‘) AS ORDERS
END-EXEC.
EXEC SQL
OPEN GET_ORDERS_UPD
END-EXEC.
ACCEPT WS-START FROM TIME.
MOVE 0 TO WS-TIME.
PERFORM PROCESS-ORDERS-UPD THRU PROCESS-ORDERS-UPD-EXIT
UNTIL WS-TIME >10000.
EXEC SQL
CLOSE GET_ORDERS_UPD
END-EXEC.
PERFORM END-TRAN THRU END-TRAN-EXIT.
TEST-PB-UPDATE-EXIT.
EXIT.
/
PROCESS-ORDERS-UPD.
ACCEPT WS-END FROM TIME.
COMPUTE WS-TIME = WS-END - WS-START.
EXEC SQL
FETCH GET_ORDERS_UPD INTO
:WS-ORDER-NBR,
:WS-AMOUNT
END-EXEC.
DISPLAY WS-ORDER-NBR, WS-AMOUNT," HAS BEEN PROCESSED".
PROCESS-ORDERS-UPD-EXIT.
EXIT.
Notice that:
*** ERROR[4001] Column "N" is not found. Tables in scope: SOS.ECUI.ORDERS. Def
ault schema: SOS.ECUI.
*** WARNING[4104] If a character literal was intended, you must use the single q
uote delimiter: ‘N‘. The use of double quotes causes SQL/MX to interpret "N" as
a delimited identifier column name.
3. Run program
$DATA01.ECSRC 44> RUN ECTEXE
00000000010000001111 HAS BEEN PROCESSED
00000000020000002222 HAS BEEN PROCESSED
00000000030000003333 HAS BEEN PROCESSED
4. Insert more records into the table
>>INSERT INTO ORDERS(ORDER_NBR,AMOUNT) VALUES(4,4444);
---1 row(s) inserted.
>>INSERT INTO ORDERS(ORDER_NBR,AMOUNT) VALUES(5,5555);
---1 row(s) inserted.
>>INSERT INTO ORDERS(ORDER_NBR,AMOUNT) VALUES(6,6666);
---1 row(s) inserted.
5. In program session, we can see the new added records
$DATA01.ECSRC 44> RUN ECTEXE
00000000010000001111 HAS BEEN PROCESSED
00000000020000002222 HAS BEEN PROCESSED
00000000030000003333 HAS BEEN PROCESSED
00000000040000004444 HAS BEEN PROCESSED
00000000050000005555 HAS BEEN PROCESSED
00000000060000006666 HAS BEEN PROCESSED
6. Check the STATUS, it has been updated to "Y"
>> SELECT * FROM SOS.ECUI.ORDERS;
ORDER_NBR AMOUNT STATUS
----------- ----------- ------
1 1111 Y
2 2222 Y
3 3333 Y
4 4444 Y
5 5555 Y
6 6666 Y
时间: 2024-10-11 13:25:58