ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Fetch into variable

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

  • Fetch into variable

    Hi i consider fetching sql query into variables. SQL returns 2 columns with 3 records (aaaaaaaa, 2; bbbbbbbb,2 and cccccccc, 2)

    im doing this:

    Code:
         d  xumowa         S                   like(UMOWA)
         d  xxx            S              2P 0
         d*
         d*****************************************************************
         D Ok              C                   0
         D
         D
          /free
           Exec Sql Declare kursor Cursor For
    
              SELECT UMOWA, count(*) LiczbaProp
              FROM library/physical_file where STATUS = 20
              GROUP BY UMOWA
              HAVING COUNT(*) > 1
              ORDER BY COUNT(*) DESC
              For Fetch Only;
    
           Exec Sql Open kursor;
           Exec Sql Fetch Into :xumowa, :xxx;
    
           Exec Sql Close kursor;
            dsply xumowa;
           *Inlr = *On;
            Return;
          /end-free
    how to do it correctly ? program compiles but displays empty field.

  • #2
    Re: Fetch into variable

    Maybe you're getting null values, Michal. What's the value of SQLSTATE?

    Also, if the result set will only have one row at most, you can use SELECT with INTO instead of a cursor.

    Comment


    • #3
      Re: Fetch into variable

      also instead of using a packed decimal field for the count(*) i would highly recommend using an integer!
      I'm not anti-social, I just don't like people -Tommy Holden

      Comment


      • #4
        Re: Fetch into variable

        Originally posted by TedHolt View Post
        Maybe you're getting null values, Michal. What's the value of SQLSTATE?

        Also, if the result set will only have one row at most, you can use SELECT with INTO instead of a cursor.
        SQLSTATE ? whats this ?
        now i tried simply version with select into and still displays nothing

        Code:
        d  xumowa         S                   like(UMOWA)
        /free
               Exec Sql 
        
                  SELECT UMOWA  
                  into :xumowa  
                  FROM physical_file
                  where STID = 2;
               
                 dsply xumowa;  
                 *inlr = *on;
                  return;             
        /end-free
        Last edited by michal2442; November 5, 2014, 08:32 AM.

        Comment


        • #5
          Re: Fetch into variable

          SQLSTATE is like %found() or %eof(). A SQLSTATE of '00000' means found, anything else is not so good. You can look up their meanings here (http://publib.boulder.ibm.com/infoce...zas2finder.htm).

          Comment


          • #6
            Re: Fetch into variable

            and question how to compile sqlrpgle ? it is enough to use crtsqlrpgi (option 14) ?

            i still display nothing, any help ?
            Last edited by michal2442; November 5, 2014, 08:50 AM.

            Comment


            • #7
              Re: Fetch into variable

              1.) Check if you have data which would be retrieved by your SQL statement.
              2.) If 1.) is yes, check if the SQL statements are throwing any errors. Either check SQLSTT and SQLCODE values in debug OR check DSPJOBLOG to see if any messages crop up.

              Comment


              • #8
                Re: Fetch into variable

                the same statement works in STRSQL command.
                dspjoblog also shows nothing

                maybe i use bad syntax ? and compiler doesnt see sql query ? please look on it
                Last edited by michal2442; November 5, 2014, 09:12 AM.

                Comment


                • #9
                  Re: Fetch into variable

                  What's the SQLCODE or SQLSTATE immediately after the SELECT ... INTO Statement?
                  SQLCODE and SQLSTATE are subfields of the SQLCA (SQL Communications Area) which is automatically included by the SQL precompiler and which is updated with each SQL statements that is executed.

                  Birgitta

                  Comment


                  • #10
                    Re: Fetch into variable

                    As others have mentioned you need to be checking for SQL errors with SqlState after each statement, that will probably point out what you are doing wrong.

                    Comment


                    • #11
                      Re: Fetch into variable

                      but how to check it ? any tutorial ?

                      Comment


                      • #12
                        Re: Fetch into variable

                        Right after you execute an SQL statement, you should check to see if there were any errors, something like this:

                        Code:
                             If sqlSTT <> sqlOK;                                              
                               jobLog('Open cursor myCursor failed due to %s' : sqlSTT);
                               *inLR = *on;                                                   
                             Endif;
                        You really should check the SQLSTATE after every SQL statement you do, like after your open, then after your fetch, etc.

                        You can define your SQL error constants something like this (I put mine in a /copy member and include it in my SQLRPGLE programs):

                        Code:
                          //-- SQLState (SQLStt) Codes                       
                        D sqlOK           c                   Const('00000') 
                        D sqlTrunc        c                   Const('01004') 
                        D sqlNullCol      c                   Const('01003') 
                        D sqlNoRow        c                   Const('02000') 
                        D sqlMultiRow     c                   Const('21000') 
                        D sqlNotOpen      c                   Const('24501') 
                        D sqlDupRow       c                   Const('42910') 
                        D sqlDupKey       c                   Const('23505')
                        You can look up what all the codes are and what they mean, but basically if you get '00000' then everything's ok. Errors like '01xxx' are warnings, and errors like '02xxx' are more serious errors.

                        If you do any inserts/updates, your first executable statements should be setting up some SQL options, especially turning off commitment control, lke this:

                        Code:
                               //-- Set SQL options
                               Exec sql
                                 SET OPTION commit = *none,
                                            datfmt = *iso;
                        Last edited by Viking; November 5, 2014, 10:53 AM.

                        Comment


                        • #13
                          Re: Fetch into variable

                          Thanks for help, today it worked i havent change anything in simpler (2nd version) version.

                          Comment


                          • #14
                            Re: Fetch into variable

                            Ah, I dont buy it.

                            have you or someone added data to the table?
                            Hunting down the future ms. Ex DeadManWalks. *certain restrictions apply

                            Comment


                            • #15
                              Re: Fetch into variable

                              Originally posted by michal2442 View Post
                              Thanks for help, today it worked i havent change anything in simpler (2nd version) version.
                              That's easily possible if you didn't sign off/on after changing the program earlier in your testing, or if you didn't reclaim an activation group. When you started a new session the next day, you could get the latest version of your program.
                              Tom

                              There are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors.

                              Why is it that all of the instruments seeking intelligent life in the universe are pointed away from Earth?

                              Comment

                              Working...
                              X