COBOL中调用SQL语句的格式:
嵌入式SQL
主变量:即可为COBOL所用,也可为SQL语句所用(前面加‘:’)。
如:DATA DIVISION.
WORKING-STORAGE SECTION.
EXEC SQL INCLUDE SQLCA END-EXEC. --通信区(必写)
EXEC SQL INCLUDE COURSERCD EXEC-EXEC. --指定数据记录
01 COURSE-DETAIL.
03 RCODE PIC X(4). --主变量,
03 RNAME PIC X(20).
03 RINST PIC X(10).
03 RDAYS PIC 9.
PROCEDURE DIVISION.
MOVE '00AB' TO RCODE.
MOVE 'GANG' TO RNAME.
MOVE 'SICC' TO RINST.
EXEC SQL
INSERT INTO Q.COURSE ( CODE, NAME)
VALUES ( :RCODE, :RNAME)
UPDATE Q.COURSE
SET INST = :RINST
WHERE CODE = :RCODE.
SELECT DAYS
INTO :RDAYS FROM Q.COURSE
WHERE CODE = :RCODE.
END-EXEC.
...
指示变量:在主变量之后,用于指示主变量所接收到数据是否为空。
当为空时,指示变量为负数。
用途:测试数据是否为空值,将空值传递给DB2中的数据。
如:EXEC SQL
SELECT NO, NAME
INTO :NO, :NAME:IND1 --测试空值
FROM TESTB
WHERE NO = :NO
END-EXEC.
IF IND1 < 0 MOVE 'UNKNOW' TO TESTATTR1.
...
MOVE -1 TO IND1.
MOVE '00A1' TO NO.
EXEC SQL
SET NAME = :NAME:IND1 --存储空值
WHERE NO = :NO
ENDE-EXEC.
动态SQL:将SQL首先读入变量,通过嵌入式SQL执行变量中的SQL。
包含SELECT和非SELECT(含有参数和不含有参数)
不含有参数的SELECT:
如:01 STMT.
49 STMT-LEN PIC S9(4) COMP VALUE +255.
49 STMT-TEXT PIC X(255).
MOVE 'DELETE FROM TESTTB'.
EXEC SQL
EXECUTE IMMEDIATE :STMT --执行
END-EXEC
含有参数的SELECT:
如:01 STMT.
49 STMT-LEN PIC S9(4) COMP VALUE +255.
49 STMT-TEXT PIC X(255).
01 PARMA PIC X(5).
01 PARMB PIC X(20).
MOVE 'DELETE FROM TESTTB
WHERE TESTNO = ? AND TESTNAME = ?' TO STMT.
EXEC SQL
PREPARE PRST FROM :STMT. ---预执行
END-EXEC.
MOVE 'A0023' TO PARMA.
MOVE 'GEORGE' TO PARMB.
EXEC SQL
EXECUTE PRST USING
ARMA,ARMB --执行
END-EXEC.
游标:对多行数据进行处理时,以行为单位的数据记录的定位功能。
分为基本游标和回滚游标(静态和动态)。
基本游标
定义:
如:MOVE 'ADFADADF' TO HATTR1.
EXEC SQL
DECLARE P1 CURSOR FOR --定义游标
SELECT TESTNO, TESTNAME --选择指定列(属性)
FROM TESTTB --指定表
WHERE TESTATTR1 = :HATTR1 -指定匹配属性
ORDER BY TESTNO -指定属性排序,如不指定默认数据记录的位置进行搜索
OPTIMIZE FOR 20 ROWS -每屏中显示的行数
FETCH FIRST 200 ROWS ONLY -共显示的行数
END-EXEC
用法:打开--使用--关闭。
如:01 HOST.
05 HNO PIC X(5).
05 HNAME PIC X(20).
...
EXEC SQL DECLARE TST CURSOR FOR
SELECT TESTNO, TESTNAME
FROM TESTTB
WHERE TESTATTR1 = :HATTR1
ORDER BY TESTNO
END-EXEC
...
EXEC SQL OPEN TST END-EXEC.
...
EXEC SQL
FETCH TST INTO :HNO, :HNAME --主要用FETCH来操作变量传递
END-EXEC.
EXEC SQL
DELETE FROM TESTTB WHERE CURRENT OF TST
END-EXEC.
...
EXEC SQL CLOSE TST END-EXEC.
STOP RUN.
当进行UPDATE操作时,要在游标中指定更新哪一个属性。
如:EXEC SQL DECLARE TST CURSOR FOR
SELECT...
...
FOR UPDATE OF TESTNAME --指定属性
...
END-EXEC
...
EXEC SQL UPDATE TESTTB
SET TESTNAME = :HNAME
WHERE CURRENT OF TST
END-EXEC.
利用游标处理多行记录:
利用游标处理多行记录时,相应的主变量也应定义为数组的形式。
通过OCCURS语句定义为COBOL中的表。
如:01 HOST-VARY.
05 HOST-ID PIC 9(5) COMP OCCURS 10 TIMES.
05 HOST-NAME OCCURS 10 TIMES.
49 NAME-LEN PIC 9(2) COMP.
49 NAME-TEXT PIC X(20).
EXEC SQL
DECLARE MTR SCROLL CURSOR
WITH ROWSET POSITIONING
FOR SELECT ID, NAME
FROM TESTTABLE
END-EXEC
EXEC SQL OPEN MTR END-EXEC.
...
EXEC SQL
FETCH CURRENT ROWSET FROM C1 FOR 10 ROWS INTO ...
END-EXEC.
EXEC SQL
FETCH NEXT ROWSET FROM C1 FOR 10 ROWS INTO...
END-EXEC.
EXEC SQL
FETCH ROWSET STARTING AT RELATIVE -7
FROM C1 FOR 10 ROWS INTO ...
END-EXEC
EXEC SQL
FETCH FIRST ROWSET
FROM MTR FOR 10 ROWS
INTO :HOST-ID:INDICATE1, :HOST-NAME:INDICATE2
END-EXEC.
...
EXEC SQL CLOSE MTR END-EXEC.
STOP RUN.