ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Embedded SQL and a nested loop

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Embedded SQL and a nested loop

    I'm trying to figure out what I am doing wrong here... In the code below, I am getting some order information within a DOU loop. Within that loop, I'm then using that information to read another file to get the "license plate" field. After that, I insert the record on a new file. I don't know how many records there may be. So, if the license plate file gets SQL = 100, I'm assuming that I've finished that order-suffix-line-count and go back to my outer loop to get the next record.

    The first loop through works. It finds the information on file 2 (LICPLTEPF) and ends the loop. On the next attempt, I'm getting SQLCOD 100 - although I know that the record is on the file. I've run it online with STRSQL and it works.

    To recap: I need to loop through file 1 to get the ordered line item, loop through file 2 to get the license plate number (if ordered qty = 2, there will be 2 license plate records), and then update file 3.

    I'm sure it's either a stupid mistake or there is an easier way to do this! If anyone has any ideas, please let me know. Since it's been raining daily here for over a week, I think my brain is waterlogged!

    Thank you.

    PHP Code:
        //===============================================================
           
    BegSr $100_GetHdrDtls;   // Get Order Hdr/Dtls
           //===============================================================

           // Loop thru Order Hdr/Dtls by Input Parms

             // Declare Cursor C1 - Find OrHeadrPF/ORDTAILPF Records by ASL

           
    Exec SQL
             
    Declare C1HDRDTLS cursor for
                
    Select A.*, B.* , C.*
                
    from ORHeadrPF A Inner Join ORDtailPF B
                On A
    .AEA4CD  B.AGA4CD and      /* Hdr PLT ID   */
                   
    A.AEA5CD  B.AGA5CD and      /* Hdr ACK NBR  */
                   
    A.AEA2NB  B.AGA2NB          /* Hdr ACK SUF  */
                
    JOIN SCFGSCHPF C
                On
                  A
    .AEA4CD  C.ALBVCD and
                  
    A.AEA5CD  C.ALDWCD and
                  
    A.AEA2NB  C.ALB3NB and
                  
    B.AGBCNB  C.ALB4NB
                where A
    .AEA4CD  = :CurPlt   and  /* Parm Plant   */
                      
    A.AEA3NB  = :PLoad  and  /* Parm Load     */
                      
    A.AEA6CD  = :PStop  and  /* Parm Stop     */
                      
    A.AEANDT  = :PLoadDte and  /* load date     */
                      
    C.ALDTCD  = :PAsyLn  and
                     (
    AEA1ST 'R' or AEA1ST 'P'/* hdr Status */
                
    Order by A.AEA4CD,              /* Hdr PLT ID   */
                         
    C.ALDTCD,              /* ASY LINE */
                         
    A.AEA3NB,              /*OFS LOAD*/
                         
    A.AEA6CD DESC,          /*STOP */
                         
    SUBSTR(A.AELKCD,1,2);   /*SEQUENCE*/
             
    Exec SQL
                Open C1HDRDTLS
    ;

             
    OrdEOF 'N';

             
    // Process the cursor

             
    DoU OrdEOF 'Y';
               
    CLEAR ORHEADRDS;
               
    CLEAR ORDTAILDS;
               
    CLEAR SCFGSCHDS;

               
    EXEC SQL
                  FETCH next from C1HDRDTLS into 
    :ORHEADRds, :ORDTAILds, :SCFGSCHds;

               
    // Test for EOF

               
    If SqlCod <> 0;
                  
    OrdEOF 'Y';
                  
    ITER;                   
               EndIf;

               
    k1_ACK AEA5CD;
               
    k1_Suffix AEA2NB;


               
    // Determine Difficulty Code
                      //       ....some calculations here


               // get all records for order/suffix/line
               //from LP file.... from 1 to 9999
              
    EXEC SQL
               
    DECLARE C2LICPLTE Cursor for
                 
    Select from LICPLTEPF
                 Where   LPLPLT 
    = :CurPlt and
                         
    LPLACK = :AEA5CD  and
                         
    LPLSUF = :AEA2NB  and
                         
    LPLLNE = :AGBCNB  and
                         
    LPLASYLN = :ALDTCD and
                         
    LPLLPNBR = :LicPltIdx;

               
    EXEC SQL
                 OPEN C2LICPLTE
    ;

               
    LicPltIdx 1;

                   
    DOU LicPltIdx 9998;
                      
    Exsr $220_Insert;  // Insert Records
                   
    ENDDO;

               
    EXEC SQL
                 CLOSE C2LICPLTE
    ;
       
    EndDo;

             
    EXEC SQL
               Close C1HDRDTLS
    ;



            
    EndSr;

        
    BegSr $220_Insert;  // Insert Records

              
    clear LICPLTEDS;

               
    EXEC SQL
                 FETCH next from C2LICPLTE into 
    :LICPLTEds ;

               If 
    SqlCod <> 0;
                  
    LicPltIdx 9999;
                  
    LeaveSR;
               EndIf;


             
    // Make Records Changes 

  • #2
    try adding a close b4 your LeaveSR of cursor C2LICPLTE
    All my answers were extracted from the "Big Dummy's Guide to the As400"
    and I take no responsibility for any of them.

    www.code400.com

    Comment


    • #3
      Jamie,
      Thank you, but that wasn't it. I just know it has to be something like that, though -- I've missed a semi-colon somewhere!

      Thank you!!

      Comment


      • #4
        This looks odd...
        Code:
                  EXEC SQL
                   DECLARE C2LICPLTE Cursor for
                     Select * from LICPLTEPF
                     Where   LPLPLT = :CurPlt and
                             LPLACK = :AEA5CD  and
                             LPLSUF = :AEA2NB  and
                             LPLLNE = :AGBCNB  and
                             LPLASYLN = :ALDTCD and
                             LPLLPNBR = :LicPltIdx;
        
                   EXEC SQL
                     OPEN C2LICPLTE;
        
                   LicPltIdx = 1;
        
                    DOU LicPltIdx > 9998;
        Cursor LICPLTEPF is based on the value in LicPltIdx (it's in the where clause), but you are opening the query before you set it to a value?

        The host variables used by a cursor are sampled by the open statement. Any subsequent fetch statement then uses the values as they were at open. The cursor does not see or care about any changes to those values after the open

        Comment


        • Vectorspace
          Vectorspace commented
          Editing a comment
          Typo - cursor name is C2LICPLTE, not LICPLTEPF

      • #5
        Originally posted by Vectorspace View Post
        This looks odd...
        Code:
        EXEC SQL
        DECLARE C2LICPLTE Cursor for
        Select * from LICPLTEPF
        Where LPLPLT = :CurPlt and
        LPLACK = :AEA5CD and
        LPLSUF = :AEA2NB and
        LPLLNE = :AGBCNB and
        LPLASYLN = :ALDTCD and
        LPLLPNBR = :LicPltIdx;
        
        EXEC SQL
        OPEN C2LICPLTE;
        
        LicPltIdx = 1;
        
        DOU LicPltIdx > 9998;
        Cursor LICPLTEPF is based on the value in LicPltIdx (it's in the where clause), but you are opening the query before you set it to a value?

        The host variables used by a cursor are sampled by the open statement. Any subsequent fetch statement then uses the values as they were at open. The cursor does not see or care about any changes to those values after the open
        OH! I get it! <lightbulb> I had that backward. I needed to set LicPltIdx BEFORE the open, not after! See? I knew it was a dumb error. I still have some issues with my program, but that gets me back on track. Thank you!

        Comment


        • Rocky
          Rocky commented
          Editing a comment
          It's always the little things that drives one crazy....

      • #6
        It's always the little things that get us...

        Comment


        • #7
          Hi All,

          I have a scenario and I need to know if that is possible in embedded DB2 SQL,


          I have a variable from a file which contains SQL statement I need to run that and apply SQL subtrings to the same and move it to local variable. I wanted to do it with one single cursor and a for loop for processing substring of results fields from cursor.

          Can anyone tell me is it possible and how can I do that?

          Comment


          • #8
            With dynamic SQL it should be possible, but without more information I only can guess what you want.

            Birgitta

            Comment


            • #9
              B.Hauser I have a table - CONFIG table which will contain SQL statement. I will read the table in my SQLRPGLE program get the SQL statement in a local variable of size 5000. Then I will prepare it and start fetching each rows by using cursors. I dont know what query will be there inside the local variable. In the same table I have column positions and column length to be applied on the each record of query result and write it on screen.

              Example: ABCDEFGHIJKL is my first row of query result

              I have Col1Pos = 1 Col1Length = 10
              Col2Pos = 15 Col2Length = 20

              These position and length will be configured in CONFIG table. I need to apply on tat query and write into screen as

              Window Prompt


              ABCDEFGHIJ KL


              I need to do this with cursor and cursor FOR loop. Is it possible ?

              Comment

              Working...
              X