VSAM操作笔记_COBOL操作VSAM

 

下载: VSAM操作笔记.doc

 

1. 概要

1.1  VSAM文件  ① 创建 ② 初始化 ③ 删除 ④打印
1.2  使用命令直接对VSAM 进行操作
1.3  COBOL中对VSAM文件的记录的 CRUD 

 

2. VSAM文件  ① 创建 ② 初始化 ③ 删除

重点: VSAM文件创建完后, 必须初始化, 即给VSAM文件装载记录
注意: 作业名以 userid 开头, 否则 需在 SDSF Menu 查看 Status Of jobs时, 需以 “S 作业名” 查看
即, 当用户查看作业作业, 进入Status Of jobs 后, 看到的是以 userid 开头的作业,
如, 你的userid=S10, 而你的jobname为 S11VSAMJ, 进入Status Of jobs 后是看不到的S11VSAMJ的.
查看的方式是: S  S11VSAMJ
2.1 创建VSAM
2.1.1 JCL
//S10DEFVS    JOB  (TESTING),'S10',CLASS=A,            
//          MSGCLASS=H,MSGLEVEL=(1,1),NOTIFY=&SYSUID 
//DEFVSAM  EXEC PGM=IDCAMS                           
//SYSPRINT DD SYSOUT=*                               
//SYSIN    DD *                                      
    DEFINE CLUSTER ( +                               
             NAME(S10.VS) +                          
             RECSZ(80 80) +                          
             RECORDS(1000 2) +                       
             KEYS(2 0) +                             
             VOL(DMTD04))                            
/*                           
 


 
2.1.2 说明
NAME(S10.VS)VSAM name, 必须以 userid (用户id)开头,如 userid=S10, 则文件名为 S10.VS
RECSZ(80 80)record 的长度为 80, 固定长度
KEYS(2 0)KEY 的长度为2 , 从记录的第一个字符开始
VOL(DMTD04)在DMTD04 卷 上创建VSAM文件
 
 
2.1.3 执行结果
  
 
2.2 初始化 VSAM
2.2.1 JCL
//S10INIT   JOB  (TESTING),'S10',CLASS=A,             
//          MSGCLASS=H,MSGLEVEL=(1,1),NOTIFY=&SYSUID 
//SORTVSAM EXEC PGM=SORT                             
//SYSOUT   DD   SYSOUT=*                             
//SORTIN   DD *                                      
00 WU QIN FEI                                        
/*                                                   
//SORTOUT  DD DSN=S10.VS,DISP=OLD                    
//SYSIN    DD *                                      
  SORT FIELDS=COPY                                   
  OUTREC FIELDS=(1,80)                               
/*     




2.2.2 说明
通过 流内数据集的方式 给 VSAM文件装载数据
可通过命令查看/修改 VSAM中的数据, 见第三章.
2.2.3 补充: 将其它文件数据装载进VSAM进行初始化
//S10INIT2  JOB  (TESTING),'S10',CLASS=A,             
//          MSGCLASS=H,MSGLEVEL=(1,1),NOTIFY=&SYSUID 
//****************************************************          
//*   INIT VSAM file with other file (S10.BPG.FILE1)
//****************************************************
//STEP01    EXEC PGM=IDCAMS              
//SYSPRINT  DD SYSOUT=*                  
            REPRO +                      
              INDATASET(S10.BPG.FILE1) + 
              OUTDATASET(S10.VS)   
/*    

                  


2.3 删除VSAM
//S10DEL    JOB  (TESTING),'S10',CLASS=A,             
//          MSGCLASS=H,MSGLEVEL=(1,1),NOTIFY=&SYSUID  
//DEFVSAM  EXEC PGM=IDCAMS                            
//DD1      DD   VOL=SER=DMTD04,UNIT=3390,DISP=OLD     
//SYSPRINT DD SYSOUT=*                                
//SYSIN    DD *                                       
  DELETE -                                            
    S10.VS      -                                     
    FILE(DD1)   -                                     
    PURGE       -                                     
    ERASE                                             
                                                      
/*       

             


2.4 打印VSAM
//S10PRINT JOB  (TESTING),'S10',CLASS=A,               
//          MSGCLASS=H,MSGLEVEL=(1,1),NOTIFY=&SYSUID   
//PRITVS   EXEC PGM=IDCAMS                             
//FILE1    DD DSN=S10.VS,DISP=SHR                      
//SYSPRINT DD SYSOUT=*                                 
//SYSIN    DD *                                        
   PRINT  INFILE(FILE1) CHAR                           
/*    

                                                                  




3. 使用命令直接对VSAM 进行操作

 
3.1 ditto ve/vb --浏览/修改 VSAM
    ditto ve  userid.VS    ---修改
    ditto vb  userid.VS    ---浏览
  
3.2 del --删除VSAM
del userid.VS          ---删除
 

4. COBOL中对VSAM文件记录的 CRUD

4.1 read, 读指定key值的记录
4.1.1 COBOL
       IDENTIFICATION DIVISION.                  
       PROGRAM-ID. VSREAD.                       
      *                                          
       ENVIRONMENT DIVISION.                     
       INPUT-OUTPUT SECTION.                     
       FILE-CONTROL.                             
           SELECT IN-FILE ASSIGN INDD            
               RECORD KEY IS IN-ID               
               ORGANIZATION IS INDEXED           
               ACCESS MODE IS RANDOM             
               FILE STATUS IS IN-FILE-STATUS.    
      *                                          
       DATA DIVISION.                            
       FILE SECTION.                              
       FD  IN-FILE.                               
       01  IN-RECORD.                             
           03 IN-ID       PIC X(02).              
           03 IN-NAME     PIC X(18).              
           03 FILLER      PIC X(60).              
       WORKING-STORAGE SECTION.                   
       77  IN-FILE-STATUS PIC XX.                 
      *                                           
       PROCEDURE DIVISION.                        
           OPEN INPUT IN-FILE.                    
           DISPLAY "OPEN STATUS: " IN-FILE-STATUS.
       100-READ.                                  
           MOVE '00' TO IN-ID.                    
      *    READ IN-FILE INTO IN-RECORD   ***WARN  
           READ IN-FILE                           
             KEY IS IN-ID                                    
             INVALID KEY                                     
               DISPLAY "INVALID KEY"                         
               GO TO 100-END                                 
             NOT INVALID KEY                                 
               DISPLAY "READ ID ", IN-ID                     
               IF IN-ID = '00'                               
               THEN                                          
                 DISPLAY 'ID ', IN-ID, ' NAME IS ', IN-NAME  
                 GO TO 100-END                               
               END-IF                                        
            END-READ.                                        
       100-END.                                              
           CLOSE IN-FILE.                                    
           DISPLAY "CLOSE STATUS: " IN-FILE-STATUS.          
           STOP RUN.      

                                   
 
 
4.1.2 JCL  -- 使用VSAM
//S10RJ     JOB  (TESTING),'S10',CLASS=A,            
//          MSGCLASS=H,MSGLEVEL=(1,1),NOTIFY=&SYSUID 
//STEP001   EXEC PGM=VSREAD                                                 
//STEPLIB   DD DSN=S10.LOADLIB,DISP=SHR              
//INDD      DD DSN=S10.VS,DISP=SHR                   
//SYSPRINT  DD SYSOUT=*                              
//SYSOUT    DD SYSOUT=*       

                       
 
 
4.2 write, 写入一条记录
       IDENTIFICATION DIVISION.                   
       PROGRAM-ID. VSWRITE.                        
      *     
       ENVIRONMENT DIVISION.                      
       INPUT-OUTPUT SECTION.                      
       FILE-CONTROL.                              
           SELECT IN-FILE ASSIGN INDD             
           RECORD KEY IS IN-ID                    
           ORGANIZATION IS INDEXED                
           ACCESS MODE IS RANDOM                  
           FILE STATUS IS IN-FILE-STATUS.         
      *
       DATA DIVISION.                             
       FILE SECTION.                              
       FD  IN-FILE.                               
       01  IN-RECORD.  
           03 IN-ID       PIC X(02).                    
           03 IN-NAME     PIC X(18).                 
           03 FILLER      PIC X(60).                 
       WORKING-STORAGE SECTION.                      
       77  IN-FILE-STATUS PIC XX.                    
      *    
       PROCEDURE DIVISION.                           
           OPEN I-O IN-FILE.                         
           DISPLAY "OPEN STATUS: " IN-FILE-STATUS.   
       100-READ.                                     
           MOVE '01'           TO IN-ID
           MOVE 'ZHANGSAN'     TO IN-NAME
           .                       
           WRITE IN-RECORD
             INVALID KEY                             
               DISPLAY "INVALID KEY, WRITE ERROR"                 
               GO TO 100-END                         
             NOT INVALID KEY                         
               DISPLAY "ID :" IN-ID  " NAME:" IN-NAME          
           END-WRITE.                                            
       100-END.                                                 
           CLOSE IN-FILE.                                       
           DISPLAY "CLOSE STATUS: " IN-FILE-STATUS.             
           STOP RUN.
 



 
4.3 rewrite, 修改指定key值的记录
       IDENTIFICATION DIVISION.                   
       PROGRAM-ID. VSREWRIT.                        
      *     
       ENVIRONMENT DIVISION.                      
       INPUT-OUTPUT SECTION.                      
       FILE-CONTROL.                              
           SELECT IN-FILE ASSIGN INDD             
           RECORD KEY IS IN-ID                    
           ORGANIZATION IS INDEXED                
           ACCESS MODE IS RANDOM                  
           FILE STATUS IS IN-FILE-STATUS.         
      *
       DATA DIVISION.                             
       FILE SECTION.                              
       FD  IN-FILE.                               
       01  IN-RECORD.  
           03 IN-ID       PIC X(02).                    
           03 IN-NAME     PIC X(18).                 
           03 FILLER      PIC X(60).                 
       WORKING-STORAGE SECTION.                      
       77  IN-FILE-STATUS PIC XX.                    
      *    
       PROCEDURE DIVISION.                           
           OPEN I-O IN-FILE.                         
           DISPLAY "OPEN STATUS: " IN-FILE-STATUS.   
       100-READ.                                     
           MOVE '00' TO IN-ID.                       
           READ IN-FILE              
             KEY IS IN-ID                            
             INVALID KEY                             
               DISPLAY "INVALID KEY"                 
               GO TO 100-END                         
             NOT INVALID KEY                         
               DISPLAY "READ ID ", IN-ID             
               IF IN-ID = '00'                                  
               THEN                                             
                 DISPLAY 'ID ', IN-ID, ' NAME IS ', IN-NAME     
                 MOVE 'MARK' TO IN-NAME                         
                 REWRITE IN-RECORD                              
                   INVALID KEY                                  
                     DISPLAY "INVALID KEY"                      
                     GO TO 100-END                              
                   NOT INVALID KEY                              
                     DISPLAY "REWRITE OK"                       
                 END-REWRITE                                    
               END-IF                                           
           END-READ.                                            
       100-END.                                                 
           CLOSE IN-FILE.                                       
           DISPLAY "CLOSE STATUS: " IN-FILE-STATUS.             
           STOP RUN.


 
4.4 delete, 删除指定key值的记录
       IDENTIFICATION DIVISION.                  
       PROGRAM-ID. vsdelete.                       
       ENVIRONMENT DIVISION.                     
       INPUT-OUTPUT SECTION.                     
       FILE-CONTROL.                             
           SELECT IN-FILE ASSIGN INDD            
           RECORD KEY IS IN-ID                   
           ORGANIZATION IS INDEXED               
           ACCESS MODE IS RANDOM                 
           FILE STATUS IS IN-FILE-STATUS.        
       DATA DIVISION.                            
       FILE SECTION.                             
       FD  IN-FILE.                              
       01  IN-RECORD.                            
           03 IN-ID       PIC X(02).                             
           03 IN-NAME     PIC X(18).                          
           03 FILLER      PIC X(60).                          
       WORKING-STORAGE SECTION.                               
       77  IN-FILE-STATUS PIC XX.                             
       PROCEDURE DIVISION.                                    
           OPEN I-O IN-FILE.                                  
           DISPLAY "OPEN STATUS: " IN-FILE-STATUS.            
       100-READ.                                              
           MOVE '00' TO IN-ID.                                
           READ IN-FILE                        
             KEY IS IN-ID                                     
             INVALID KEY                                      
               DISPLAY "INVALID KEY"                          
               GO TO 100-END                                  
             NOT INVALID KEY                                  
               DISPLAY "READ ID ", IN-ID                      
               IF IN-ID = '00'                                  
               THEN                                             
                 DISPLAY 'ID ', IN-ID, ' NAME IS ', IN-NAME     
                 MOVE 'MARK' TO IN-NAME                         
                 DELETE IN-FILE                                 
                   INVALID KEY                                  
                     DISPLAY "INVALID KEY"                      
                     GO TO 100-END                              
                   NOT INVALID KEY                              
                     DISPLAY "DELETE OK"                        
                 END-DELETE                                     
               END-IF                                           
           END-READ.                                            
       100-END.                                                 
           CLOSE IN-FILE.                                       
           DISPLAY "CLOSE STATUS: " IN-FILE-STATUS.             
           STOP RUN. 


 
4.5 start, read next,  定位到指定key值记录, 顺序往下读完.


       IDENTIFICATION DIVISION.                            
       PROGRAM-ID. vsstart.                                
       ENVIRONMENT DIVISION.                               
       INPUT-OUTPUT SECTION.                               
       FILE-CONTROL.                                       
           SELECT IN-FILE ASSIGN INDD                      
               RECORD KEY IS IN-ID                             
               ORGANIZATION IS INDEXED                         
               ACCESS MODE IS DYNAMIC                          
               FILE STATUS IS IN-FILE-STATUS.                  
       DATA DIVISION.                                      
       FILE SECTION.                                       
       FD  IN-FILE.                                        
       01  IN-RECORD.                                      
           03 IN-ID       PIC X(02).                                
           03 IN-NAME     PIC X(18).                             
           03 FILLER      PIC X(60).                             
       WORKING-STORAGE SECTION.                                  
       77  IN-FILE-STATUS PIC XX.                                
       PROCEDURE DIVISION.                                       
           OPEN I-O IN-FILE.                                     
           DISPLAY "OPEN STATUS: " IN-FILE-STATUS.               
       100-START. 
           display "start at  01".
           
           MOVE '01' TO IN-ID                                    
           START IN-FILE                                         
             KEY >= IN-ID                                        
             INVALID KEY                                         
               DISPLAY "INVALID KEY"                             
               GO TO 100-END                                     
             NOT INVALID KEY                                     
               DISPLAY "START OK"                                
           END-START.                                     
       100-READ.                                          
           READ IN-FILE NEXT RECORD 
             AT END                                       
               DISPLAY "FILE IS END"                      
               GO TO 100-END                              
             NOT AT END                                   
               DISPLAY 'ID ', IN-ID, ' NAME IS ', IN-NAME 
               GO TO 100-READ                             
           END-READ.                                      
       100-END.                                           
           CLOSE IN-FILE.                                 
           DISPLAY "CLOSE STATUS: " IN-FILE-STATUS.       
           STOP RUN.     







你可能感兴趣的:(VSAM操作笔记,COBOL操作VSAM)