SQL 查询
假设我们有一个名为“Student”的表,其中包含 Student-Id、Student-Name 和 Student-Address。
STUDENT 表包含以下数据 -
Student Id Student Name Student Address
1001 Mohtashim M. Hyderabad
1002 Nishant Malik Delhi
1003 Amitabh Bachan Mumbai
1004 Chulbul Pandey Lucknow
下面的例子展示了SELECT在 COBOL 程序中查询 -
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
EXEC SQL
INCLUDE SQLCA
END-EXEC.
EXEC SQL
INCLUDE STUDENT
END-EXEC.
EXEC SQL BEGIN DECLARE SECTION
END-EXEC.
01 WS-STUDENT-REC.
05 WS-STUDENT-ID PIC 9(4).
05 WS-STUDENT-NAME PIC X(25).
05 WS-STUDENT-ADDRESS X(50).
EXEC SQL END DECLARE SECTION
END-EXEC.
PROCEDURE DIVISION.
EXEC SQL
SELECT STUDENT-ID, STUDENT-NAME, STUDENT-ADDRESS
INTO :WS-STUDENT-ID, :WS-STUDENT-NAME, WS-STUDENT-ADDRESS FROM STUDENT
WHERE STUDENT-ID=1004
END-EXEC.
IF SQLCODE = 0
DISPLAY WS-STUDENT-RECORD
ELSE DISPLAY 'Error'
END-IF.
STOP RUN.
JCL执行上述 COBOL 程序 -
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP001 EXEC PGM = IKJEFT01
//STEPLIB DD DSN = MYDATA.URMI.DBRMLIB,DISP = SHR
//SYSPRINT DD SYSOUT=*
//SYSUDUMP DD SYSOUT=*
//SYSOUT DD SYSOUT=*
//SYSTSIN DD *
DSN SYSTEM(SSID)
RUN PROGRAM(HELLO) PLAN(PLANNAME) -
END
/*
当您编译并执行上述程序时,它会产生以下结果 -
1004 Chulbul Pandey Lucknow
下面的例子展示了INSERT在 COBOL 程序中查询 -
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
EXEC SQL
INCLUDE SQLCA
END-EXEC.
EXEC SQL
INCLUDE STUDENT
END-EXEC.
EXEC SQL BEGIN DECLARE SECTION
END-EXEC.
01 WS-STUDENT-REC.
05 WS-STUDENT-ID PIC 9(4).
05 WS-STUDENT-NAME PIC X(25).
05 WS-STUDENT-ADDRESS X(50).
EXEC SQL END DECLARE SECTION
END-EXEC.
PROCEDURE DIVISION.
MOVE 1005 TO WS-STUDENT-ID.
MOVE 'TutorialsPoint' TO WS-STUDENT-NAME.
MOVE 'Hyderabad' TO WS-STUDENT-ADDRESS.
EXEC SQL
INSERT INTO STUDENT(STUDENT-ID, STUDENT-NAME, STUDENT-ADDRESS)
VALUES (:WS-STUDENT-ID, :WS-STUDENT-NAME, WS-STUDENT-ADDRESS)
END-EXEC.
IF SQLCODE = 0
DISPLAY 'Record Inserted Successfully'
DISPLAY WS-STUDENT-REC
ELSE DISPLAY 'Error'
END-IF.
STOP RUN.
JCL执行上述 COBOL 程序 -
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP001 EXEC PGM = IKJEFT01
//STEPLIB DD DSN = MYDATA.URMI.DBRMLIB,DISP=SHR
//SYSPRINT DD SYSOUT = *
//SYSUDUMP DD SYSOUT = *
//SYSOUT DD SYSOUT = *
//SYSTSIN DD *
DSN SYSTEM(SSID)
RUN PROGRAM(HELLO) PLAN(PLANNAME) -
END
/*
当您编译并执行上述程序时,它会产生以下结果 -
Record Inserted Successfully
1005 TutorialsPoint Hyderabad
下面的例子展示了UPDATE在 COBOL 程序中查询 -
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
EXEC SQL
INCLUDE SQLCA
END-EXEC.
EXEC SQL
INCLUDE STUDENT
END-EXEC.
EXEC SQL BEGIN DECLARE SECTION
END-EXEC.
01 WS-STUDENT-REC.
05 WS-STUDENT-ID PIC 9(4).
05 WS-STUDENT-NAME PIC X(25).
05 WS-STUDENT-ADDRESS X(50).
EXEC SQL END DECLARE SECTION
END-EXEC.
PROCEDURE DIVISION.
MOVE 'Bangalore' TO WS-STUDENT-ADDRESS.
EXEC SQL
UPDATE STUDENT SET STUDENT-ADDRESS=:WS-STUDENT-ADDRESS
WHERE STUDENT-ID = 1003
END-EXEC.
IF SQLCODE = 0
DISPLAY 'Record Updated Successfully'
ELSE DISPLAY 'Error'
END-IF.
STOP RUN.
JCL执行上述 COBOL 程序 -
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP001 EXEC PGM = IKJEFT01
//STEPLIB DD DSN = MYDATA.URMI.DBRMLIB,DISP = SHR
//SYSPRINT DD SYSOUT = *
//SYSUDUMP DD SYSOUT = *
//SYSOUT DD SYSOUT = *
//SYSTSIN DD *
DSN SYSTEM(SSID)
RUN PROGRAM(HELLO) PLAN(PLANNAME) -
END
/*
当您编译并执行上述程序时,它会产生以下结果 -
Record Updated Successfully
以下example显示的用法DELETE在 COBOL 程序中查询 -
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
EXEC SQL
INCLUDE SQLCA
END-EXEC.
EXEC SQL
INCLUDE STUDENT
END-EXEC.
EXEC SQL BEGIN DECLARE SECTION
END-EXEC.
01 WS-STUDENT-REC.
05 WS-STUDENT-ID PIC 9(4).
05 WS-STUDENT-NAME PIC X(25).
05 WS-STUDENT-ADDRESS X(50).
EXEC SQL END DECLARE SECTION
END-EXEC.
PROCEDURE DIVISION.
MOVE 1005 TO WS-STUDENT-ID.
EXEC SQL
DELETE FROM STUDENT
WHERE STUDENT-ID=:WS-STUDENT-ID
END-EXEC.
IF SQLCODE = 0
DISPLAY 'Record Deleted Successfully'
ELSE DISPLAY 'Error'
END-IF.
STOP RUN.
JCL执行上述 COBOL 程序 -
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP001 EXEC PGM = IKJEFT01
//STEPLIB DD DSN = MYDATA.URMI.DBRMLIB,DISP=SHR
//SYSPRINT DD SYSOUT = *
//SYSUDUMP DD SYSOUT = *
//SYSOUT DD SYSOUT = *
//SYSTSIN DD *
DSN SYSTEM(SSID)
RUN PROGRAM(HELLO) PLAN(PLANNAME) -
END
/*
当您编译并执行上述程序时,它会产生以下结果 -
Record Deleted Successfully