DFU Query SQL

25. SQL�p�b�P�[�W���� = RPG ��SQL���g�p����ɂ́H

RPG��C/400 �̓�����SQL��g�ݍ��񂾂��̂��uSQL�p�b�P�[�W�v�ƌĂԁB
���m�ɂ� CRTSQLPKG�R�}���h �ɂ���č쐬������I�u�W�F�N�g��SQL �p�b�P�[�W��
���邪�A�P��RPG �� COBOL�AC/400 �Ȃǂ̍��������SQL���𖄂ߍ��񂾂��̂������ł�
SQL�p�b�P�[�W�ƌĂԁB

�]����RPG�J���ɔ�ׂāASQL�p�b�P�[�W�̊J���ɂ����ĈقȂ�̂�

�P�Ȃ�RPG�ł͂Ȃ��ASQL�p�b�P�[�W�ɂ���ĊJ�����闘�_�͊T�ˎ��̂Ƃ���ł���B

����A�Z�����\���A�����������K�v������B

����ɂ��Ă��ASQL�p�b�P�[�W�͏_��œ��I�ȏ������J���ł���Ƃ����_�ɂ����Ă͖��͓I��
����RPG�����ł͂��Ȃ荢��ł������J���� SQL�ł���Ηe�ՂɎ����”\�ł���ꍇ������B
�J���̉”\�������Ȃ�g�債�Ă����Ƃ����ϓ_���� SQL�̗��p�͏\���A�����ɒl����
���̂ł���B

���đO�u���̒m���͂��ꂭ�炢�ɂ��āASQL�ɂ��J���̋�̗���Љ�悤�B
SQL�̎g�p����藝�����Ē������߂ɁA�ŏ��͓`���I��RPG�T�C�N�����g��������̗�Ƃ���
�uRPG001�v�Ƃ����v���O�������Љ��B
RPG001 �͏��i�}�X�^�[(QTRFIL/SHOHIN) �̕i��R�[�h���̘_���t�@�C��
(QTRFIL/LSHOHNS) ��ǂ�ŁA�i��R�[�h�ʂɉ��y�[�W���Ĉ������P���ȗ�ł���B

�y RPG001: �i��ʏ��i�}�X�^�[�ꗗ�\ �z
�g�p�f�[�^�E�x�[�X SHOHIN.MBR LSHOHINS.MBR HINSHU.MBR
0001.00      H        1   Y/                                                    
0002.00      F**********  �i��ʏ��i�ꗗ�\            ************************* 
0003.00      FLSHOHNS IP  E           K        DISK                             
0004.00      FHINSHU  IF  E           K        DISK                             
0005.00      FQPRINT  O   F     132     OF    LPRINTER                          
0006.00      F***************************************************************** 
0007.00      E                    HDR     1   1 32                ���o��        
0008.00      E                    DAR         2 10                              
0009.00      LQPRINT   66FL 62OL                                                
0010.00      I@SHOHIN     01                                                    
0011.00      I                                              SHSCODL1            
0012.00      I*( �f�[�^�E�x�[�X�O���f�[�^�\�� )                                 
0013.00      IDSF001    EIDSHINSHU                                              
0014.00      I            DS                                                    
0015.00      I                                        1   5 TESCOD              
0016.00      C******************************************************            
0017.00      C           *INL1     IFEQ '1'                        *INL1        
0018.00      C******************************************************            
0019.00      C*   SHSCOD : �i�� ðn�                  �̐؂��     *            
0020.00      C                     SETOF                     40     ����        
0021.00      C                     END                             *INL1        
0022.00      C******************************************************            
0023.00      C*            ���@�@�ׁ@�@���@�@�Z                                 
0024.00      C******************************************************            
0025.00      C*----------------------------------------------------+            
0026.00      C           KEY001    KLIST                           |            
0027.00      C                     KFLD           SHSCOD           |            
0028.00      C*----------------------------------------------------+            
0029.00      C                     CLEARDSF001                      CHAIN       
0030.00      C                     SETOF                     99     CHAIN       
0031.00      C           KEY001    CHAINHINSHU               99     CHAIN       
0032.00      C*                                                                 
0033.00      C*( ���׈�� )                                                     
0034.00      C*----------------------------------------------------+            
0035.00      C                     SETON                     42    |            
0036.00      C                     EXSR OUTPUT                     | ���׈��   
0037.00      C*----------------------------------------------------+            
0038.00      C                     ADD  1         KENSU   70        ����        
0039.00      C*( ���v )                                                         
0040.00      C*                                                                 
0041.00      C           END       TAG                                          
0042.00      C*( T-LR �ŏI���v )                                              
0043.00      C*----------------------------------------------------+          
0044.00      CLR                   SETON                     49    |          
0045.00      CLR                   EXSR OUTPUT                     | ������� 
0046.00      C*----------------------------------------------------+          
0047.00      C******************************************************          
0048.00      C           *INZSR    BEGSR                                      
0049.00      C******************************************************          
0050.00      C*  ���� �bh� �݂̂̎��s                                         
0051.00      CSR                   MOVE *BLANKS   DMYCOD 15                   
0052.00      CSR         INZEND    ENDSR                                      
0053.00      C******************************************************          
0054.00      C           OUTPUT    BEGSR                                      
0055.00      C******************************************************          
0056.00      CSRN40                SETON                     4041             
0057.00      CSR                   EXCPT                                      
0058.00      CSR OF                SETOF                     40OF             
0059.00      CSR                   SETOF                     414243           
0060.00      CSR                   SETOF                     444546           
0061.00      CSR                   SETOF                     474849           
0062.00      CSR                   ENDSR                                      
0063.00      OQPRINT  E  206   41                                      
0064.00      O                         UDATE Y    8                    
0065.00      O                                   14 ' �쐬 '           
0066.00      O                         HDR,1     82                    
0067.00      O                                  128 'PAGE.'            
0068.00      O                         PAGE  Z  131                    
0069.00      O        E  1     41                                      
0070.00      O                                   12 ' �i��R�[�h '     
0071.00      O                         SHSCOD    16                    
0072.00      O                                   24 ' �i�햼 '         
0073.00      O                         HNSNAM    38                    
0074.00      O        E  1     41                                      
0075.00      O                                   12 ' ���i�R�[�h '     
0076.00      O                                   34 ' ���i�� '         
0077.00      O                                   68 ' �P�� '           
0078.00      O        E  2     42                                      
0079.00      O                         SHCODE    11                    
0080.00      O                         SHNAME    46                    
0081.00      O                         SHTANKJ   70                    
0082.00      O        E  1     49                                      
0083.00      O                                   40 ' �������� '       
0084.00      O                                   57 '. . . . . . . . .'    
0085.00      O                         KENSU 2   65                        
0086.00 **  HDR                                                            
0087.00  �i��ʏ��i�ꗗ�\      
�y PGM001 �̎��s�̌��� �z

���� PGM001 ���������A���̏�Ŏ��s���āA���̌��ʂ� HTML �Ƃ��Ă����ɂ����B
����URL���N���b�N���Ē��������B�����Spool���C�^�[Ver 3.0 �ɂ���Ĉ��JOB��
���ʂƂ��ẴX�v�[�������̏��HTML�����ĕ\������@�\�𗘗p���Ă���B

http://218.44.135.18/cgi-bin/RPG001CL

����X�v�[����HTML���������́A
�u25.����X�v�[����HTML�Ŗ߂�CLP-CGI�v���Q�Ƃ��Ē��������B

����g�p����CLP �̃\�[�X��
http://218.44.135.18/QSYS.LIB/PGMRLIB.LIB/QCLLESRC.FILE/RPG001CL.MBR�ł���B

���ɂ��悢���L��RPG�v���O������ SQL�p�b�P�[�W�ɒu���������T���v�����Љ��B
SQLRPG �̒��ɂ�

SELECT T1.SHCODE, T1.SHNAME, T1.SHTANK, T1.SHSCOD,
    T2.HNSNAM FROM QTRFIL/SHOHIN T1, QTRFIL/HINSHU T2 
    
        WHERE T1.SHSCOD = T2.HNSCOD 

        ORDER BY T1.SHSCOD, T1.SHCODE 

�Ƃ��� SQL: SELECT����g�ݍ��ށB�����2�‚̃f�[�^�E�x�[�X:
���i�}�X�^�[(QTRFIL/SHOHIN) �ƕi��}�X�^�[(QTRFIL/HINSHU) ��
�i��R�[�h�ɂ���Č��� (WHERE T1.SHSCOD = T2.HNSCOD) ���ĕi��R�[�h���A
���i�R�[�h���ɕ��� (ORDER BY T1.SHSCOD, T1.SHCODE) ����
SELECT���ł���B
RPG �̒��ɑ}������SQL���߂�

C/EXEC SQL
C+ ........... ( SQL����) ........
C+ ........... ( SQL����) ........
C/END-EXEC

�̂悤�� /EXEC SQL ���� /END-EXEC �ň͂ތ`���ɂ���ċL�q����B

�y SQL001: SQL�T���v��:�i��ʏ��i�}�X�^�[�ꗗ�\ �z
0001.00      H        1   Y/                                                   
0002.00      F********** SQL ����T���v���F�i��ʏ��i�ꗗ�\  ******************
0003.00      FQPRINT  O   F     132     OF    LPRINTER                         
0004.00      F*****************************************************************
0005.00      E                    HDR     1   1 32                ���o��       
0006.00      E                    DAR         2 10                             
0007.00      LQPRINT   66FL 62OL                                               
0008.00      I*( �f�[�^�E�x�[�X�O���f�[�^�\�� )                                
0009.00      IFMT001    E DSSHOHIN                                             
0010.00      IFMT002    E DSHINSHU                                             
0011.00      C******************************************************           
0012.00      C*    SQL ���̃J�[�\���̑O����                                    
0013.00      C******************************************************           
0014.00      C* SELECT ���ɂ���ăJ�[�\�� C1 ��p��                            
0015.00      C/EXEC SQL DECLARE C1 CURSOR FOR                                  
0016.00      C+  SELECT T1.SHCODE, T1.SHNAME, T1.SHTANK, T1.SHSCOD,            
0017.00      C+         T2.HNSNAM FROM QTRFIL/SHOHIN T1, QTRFIL/HINSHU T2      
0018.00      C+         WHERE T1.SHSCOD = T2.HNSCOD                            
0019.00      C+         ORDER BY T1.SHSCOD, T1.SHCODE                          
0020.00      C/END-EXEC                                                        
0021.00      C*  �J�[�\�����I�[�v��                                           
0022.00      C/EXEC SQL                                                       
0023.00      C+   OPEN C1                                                     
0024.00      C/END-EXEC                                                       
0025.00      C******************************************************          
0026.00      C*            ���@�@�ׁ@�@���@�@�Z                               
0027.00      C******************************************************          
0028.00      C           *LIKE     DEFN SHSCOD    SHSCOB                      
0029.00      C                     MOVE *LOVAL    SHSCOB                      
0030.00      C/EXEC SQL WHENEVER NOT FOUND GOTO EOF                           
0031.00      C/END-EXEC                                                       
0032.00      C                     DO   *HIVAL                                
0033.00      C/EXEC SQL                                                       
0034.00      C+   FETCH C1 INTO :SHCODE, :SHNAME, :SHTANK, :SHSCOD,           
0035.00      C+                 :HNSNAM                                       
0036.00      C/END-EXEC                                                       
0037.00      C*( ���׈�� )                                                   
0038.00      C           SHSCOB    COMP SHSCOD                   40           
0039.00      C*----------------------------------------------------+          
0040.00      C                     SETON                     42    |          
0041.00      C                     EXSR OUTPUT                     | ���׈�� 
0042.00      C*----------------------------------------------------+           
0043.00      C                     MOVE SHSCOD    SHSCOB                       
0044.00      C                     ADD  1         KENSU   70        ����       
0045.00      C                     END                                         
0046.00      C           EOF       TAG                                         
0047.00      C*  �J�[�\�����N���[�Y                                            
0048.00      C/EXEC SQL                                                        
0049.00      C+  CLOSE C1                                                      
0050.00      C/END-EXEC                                                        
0051.00      C           END       TAG                                         
0052.00      C*( T-LR �ŏI���v )                                               
0053.00      C*----------------------------------------------------+           
0054.00      C                     SETON                     49    |           
0055.00      C                     EXSR OUTPUT                     | �������  
0056.00      C*----------------------------------------------------+           
0057.00      C                     SETON                     LR                
0058.00      C******************************************************           
0059.00      C           *INZSR    BEGSR                                       
0060.00      C******************************************************           
0061.00      C*  �����݂̂̎��s                                                
0062.00      CSR                   CLEARFMT001                                 
0063.00      CSR                   CLEARFMT002                                 
0064.00      CSR         INZEND    ENDSR                                       
0065.00      C******************************************************           
0066.00      C           OUTPUT    BEGSR                                       
0067.00      C******************************************************           
0068.00      CSRN40                SETON                     4041              
0069.00      CSR                   EXCPT                                       
0070.00      CSR OF                SETOF                     40OF              
0071.00      CSR                   SETOF                     414243            
0072.00      CSR                   SETOF                     444546            
0073.00      CSR                   SETOF                     474849            
0074.00      CSR                   ENDSR                                       
0075.00      OQPRINT  E  206   41                                              
0076.00      O                         UDATE Y    8                            
0077.00      O                                   14 ' �쐬 '                   
0078.00      O                         HDR,1     82                            
0079.00      O                                  128 'PAGE.'                    
0080.00      O                         PAGE  Z  131                            
0081.00      O        E  1     41                                              
0082.00      O                                   12 ' �i��R�[�h '             
0083.00      O                         SHSCOD    16                            
0084.00      O                                   24 ' �i�햼 '          
0085.00      O                         HNSNAM    38                     
0086.00      O        E  1     41                                       
0087.00      O                                   12 ' ���i�R�[�h '      
0088.00      O                                   34 ' ���i�� '          
0089.00      O                                   68 ' �P�� '            
0090.00      O        E  2     42                                       
0091.00      O                         SHCODE    11                     
0092.00      O                         SHNAME    46                     
0093.00      O                         SHTANKJ   70                     
0094.00      O        E  1     49                                       
0095.00      O                                   40 ' �������� '        
0096.00      O                                   57 '. . . . . . . . .' 
0097.00      O                         KENSU 2   65                     
0098.00 **  HDR                                                         
0099.00  �i��ʏ��i�ꗗ�\                                               
�y ��� �z

�R���p�C����

CRTSQLRPG PGM(MYLIB/SQL001) SRCFILE(MYSRCLIB/QRPGSRC)    COMMIT(*NONE) RDB(*NONE)

�̂悤�Ɏw�肷��B �X�V�����͍s��Ȃ��̂ŃR�~�b�g�����g�̎w��͍s��Ȃ��B
���Ă��� SQLRPG�\�[�X�ł��邪�ASELECT���ɂ���ăf�[�^�E�x�[�X�����o���̂�
�t�@�C���d�l���ɂ̓f�[�^�E�x�[�X�̋L�q�͖����B
SELECT���ɂ���ăA�N�Z�X�E�p�X���ꎞ�I�ɍ쐬�����FETCH ���ɂ����1���R�[�h����

T1.SHCODE, T1.SHNAME, T1.SHTANK, T1.SHSCOD, T2.HNSNAM

�̊e�t�B�[���h�l�����o�����

�t�B�[���h; :SHCODE, :SHNAME, :SHTANK, :SHSCOD, :HNSNAM 

�ɖ��ߍ��܂��B
SQL���̒��ł� RPG �Ŏg�p����t�B�[���h���� MYFLD �Ƃ����t�B�[���h�� :MYFLD ��
�L�q����B

FETCH �ɂ�閄�ߍ��݂͑I�������t�B�[���h���Ƀt�B�[���h�̃^�C�v�͐��m�Ɉ�v����
���Ȃ���΂Ȃ�Ȃ��B
�Ⴆ�Ε����t�B�[���h��I�����Ă���̂ɑΉ�����̂ɑΉ�����̂������t�B�[���h�ł����
�t�B�[���h�E�^�C�v�̕s��v�Ƃ��� SQL0303 �̃G���[���߂����B
���߂�SQL�p�b�P�[�W���g�p����Ƃ��́A���S�҂͕K���Ƃ����Ă悢�قǂ��̃G���[��
�Y�܂����B(����)
���̂悤�ȃG���[��h�~���邽�߂ɁA�����ł�SELECT���Ŏg�p����f�[�^�E�x�[�X�̋L�q��

0008.00      I*( �f�[�^�E�x�[�X�O���f�[�^�\�� )    
0009.00      IFMT001    E DSSHOHIN          
0010.00      IFMT002    E DSHINSHU   

�ɂ���ĊO���L�q�Ƃ��ăC���N���[�h���Ă��邱�Ƃɒ��ӂ��ꂽ���B
2�‚̃f�[�^�E�x�[�X��
���C�u�����[�E���X�g�ɑ��݂��Ă���΃R���p�C���ɂ���Ă��̋L�q�� DS�̍�ƃt�B�[���h��
���Ď�荞�܂�邱�ƂɂȂ�B
����� FETCH �Ŏg�p���� SHCODE , .... �͐��m�Ɍ��̃f�[�^�E�x�[�X�Ɠ����^�C�v��
���ĕ�܂����̂� FETCH �ɂ��^�C�v�s��v�̃G���[�͐�΂ɋN����Ȃ��B

0032.00      C                     DO   *HIVAL 
          :
          :
0045.00      C                     END 

�ɂ���� FETCH ���J��Ԃ��ă��R�[�h���Ɉ�����s���̂ł��邪

0030.00      C/EXEC SQL WHENEVER NOT FOUND GOTO EOF  
0031.00      C/END-EXEC 

�ɂ���� FETCH �ɂ���ă��R�[�h�����‚���Ȃ��Ȃ�΃^�O : EOF �֏����͔�΂����
�����͏I������B

�y SQL001 �̎��s�̌��� �z

����ł͑����A���̎��s���ʂ����Ă݂悤�B

http://218.44.135.18/cgi-bin/SQL001CL

���x�͍ŏI�̕i��R�[�h = 0009 �̃��R�[�h���������Ă��Ȃ��͂����B
���͕i��R�[�h=0009 �͕i��}�X�^�[�ɂ͑��݂��Ȃ��G���[�E���R�[�h�ł���B
����͒P����SELECT���ɂ�錋���Ȃ̂Ō����ł��Ȃ��������R�[�h�͌������Ă��܂��B
���̂悤��SQL�����g�p����ꍇ�ɂ̓��R�[�h�̌����ɂ����ӂ��K�v�ł���B
SQL SELECT���ɂ���ă��R�[�h���������������Ȃ��ꍇ��

SELECT T1.SHCODE, T1.SHNAME, T1.SHTANK, T1.SHSCOD,
    T2.HNSNAM FROM QTRFIL/SHOHIN T1 LEFT OUTER JOIN

        QTRFIL/HINSHU T2 ON T1.SHSCOD = T2.HNSCOD

        ORDER BY T1.SHCODE ASC

�̂悤�� LEFT OUTER JOIN ... ON .... �ɂ���ċL�q���邱�Ƃ��K�v�ł���B
WHERE �ɂ��SELECT���͈�ʓI�ɕ��y���Ă���̂� WHERE ���g�p�������ł��邪
���ӂ��K�v�ł���B
Chicago Ver 5.0 �̃��[�U�[�ł���� Excel�T�[�o�[��
[�\��]-[Excel�T�[�o�[��`�r���[]�ɂ���Đ������ꂽSQL�����Q�Ƃ��邱�Ƃ��ł���B
Chiacgo Ver 5.0 ��SQL���́A���R�[�h�̒E���������悤��
LEFT OUTER JOIN ... ON .. �ɂ���Č�������Ă��邱�Ƃ��A���킩��ɂȂ�Ǝv���B