ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Read Distinct records through Openquery

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

  • Read Distinct records through Openquery

    I would like to know how can we read records using Openquery subjected to the following condiitons.

    1. Record selection based on some condition
    2. selection of distinct record in case more than one record exists for the Key filelds comibantion.

    I Tried the following but it throws exception.

    OPNQRYF FILE((RBLFMTN3)) OPTION(*ALL) QRYSLT('(TFDOS +
    *LT ZSDAT) *AND (TFDOS *GT ZEDAT)') +
    KEYFLD(*FILE) UNIQUEKEY(2) MAPFLD((ZSDAT +
    &SDAT *ZONED 8) (ZEDAT &EDAT *ZONED 8)) +

    Please help if some one knows it?.......PLs...

  • #2
    Re: Read Distinct records through Openquery

    It has been many moons since I have used QPNQRYF....
    I would recoommend you look into SQL for your answer.

    Also it seems odd that you would not include duplicate key
    records...can you explain this more in detail.

    Can you create SQLRPGLE programs?


    jf
    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
      Re: Read Distinct records through Openquery

      I think, to start with, your quotes are in the wrong places in the QRYSLT statement. You are specifiying a constant (by virtue of the quotes) instead of a compare expression.

      You want something more like

      DCL &QRYSLT

      CHGVAR &QRYSLT VALUE('(' *CAT TFDOS +
      ' *LT ' *CAT ZSDAT *CAT ') *AND (' *CAT TFDOS *CAT ' *GT ' +
      *CAT ZEDAT *CAT')')

      Then replace QRYSLT with QRYSLT(&QRYSLT)

      The trick is that the constants (such as paren, operators, etc ) must be enclosed in quotes, and the field / varibale names must NOT be enclosed. Using a &QRYSLT variable makes it easier to code, and also allows to to output and/or debug the &QRYSLT value just before the OPNQRYF command to see what its going to look like.

      This doesn't address the "distinct", but should help the "select" part.

      Comment


      • #4
        Re: Read Distinct records through Openquery

        @#%@#%@#%@#% just when I thought I was away arrow pulls me back in

        Here is an example the other way......
        It is important that you start using SQL more often and OPNQRYF less.
        I think that ist not even supported anymore by IBM. But what is

        anyway

        PHP Code:
                     PGM        PARM(&COMP)

                     
        DCL        VAR(&COMP)   TYPE(*CHARLEN(2)
                     
        DCL        VAR(&FRMGL#) TYPE(*CHAR) LEN(10)
                     
        DCL        VAR(&TOGL#)  TYPE(*CHAR) LEN(10)
                     
        DCL        VAR(&FRDATCTYPE(*CHARLEN(8)
                     
        DCL        VAR(&TODATCTYPE(*CHARLEN(8)
                     
        DCL        VAR(&FRDATZTYPE(*CHARLEN(8)
                     
        DCL        VAR(&TODATZTYPE(*CHARLEN(8)
                     
        DCL        VAR(&ERRFLGTYPE(*CHARLEN(1)

            
        /*  RETRIEVE PARMS FROM LOCAL DATA AREA  AS CHARACTER */
                     
        RTVDTAARA  DTAARA(*LDA (1 6)) RTNVAR(&FRDATC)
                     
        RTVDTAARA  DTAARA(*LDA (7 6)) RTNVAR(&TODATC)
                     
        RTVDTAARA  DTAARA(*LDA (13 10)) RTNVAR(&FRMGL#)
                     
        RTVDTAARA  DTAARA(*LDA (23 10)) RTNVAR(&TOGL#)

                     
        IF         COND(&FRDATC *EQ '000000'THEN(DO)
                     
        CHGVAR     VAR(&FRDATZVALUE('0000000')
                     
        ENDDO
                     
        ELSE       CMD(DO)
                     
        CALL       PGM(CVTDTC0PARM(&FRDATC &FRDATZ '*MDY' +
                                  
        '*CYMD' &ERRFLG)
                     
        ENDDO

                     
        IF         COND(&TODATC *EQ '000000' *OR &TODATC *EQ +
                                  
        '999999'THEN(DO)
                     
        CHGVAR     VAR(&TODATZVALUE('9999999')
                     
        ENDDO
                     
        ELSE       CMD(DO)
                     
        CALL       PGM(CVTDTC0PARM(&TODATC &TODATZ '*MDY' +
                                  
        '*CYMD' &ERRFLG)
                     
        ENDDO

                     
        IF         COND(&COMP '20'THEN(DO)
                     
        OVRPRTF    FILE(QSYSPRTOUTQ(RPRT2001)
                     
        ENDDO
                     
        IF         COND(&COMP '30'THEN(DO)
                     
        OVRPRTF    FILE(QSYSPRTOUTQ(RPRT3006)
                     
        ENDDO
                     
        IF         COND(&COMP '72'THEN(DO)
                     
        OVRPRTF    FILE(QSYSPRTOUTQ(RPRT7206)
                     
        ENDDO

                     OVRDBF     FILE
        (APDCDISKTOFILE(APDCDISSHARE(*YES)

                     
        OPNQRYF    FILE((APDCDIS) (APAVEND)) FORMAT(APDCDISK) +
                                  
        QRYSLT('GL# = %RANGE(' *CAT &FRMGL# *BCAT +
                                  
        &TOGL# *CAT ') *AND ADCKDT = %RANGE(' +
                                  
        *CAT &FRDATZ *BCAT &TODATZ *CAT ')') +
                                  
        KEYFLD((ADGL#1) (ADGL#2) (ADGL#3) +
                                  
        (ADGL#4) (AAALPH) (ADVEN#) (ADCKDT) +
                                  
        (ADCHK#)) JFLD((1/ADVEN# 2/AAVEN#)) +
                                  
        MAPFLD((GL# '(ADGL#1 * 10000000) + +
                                  
        (ADGL#2 * 100000) + (ADGL#3 * 100) + +
                                  
        ADGL#4')) ALWCPYDTA(*OPTIMIZE)

                     
        CALL       PGM(APA33PARM(&COMP)

                     
        CLOF       OPNID(APDCDIS)

                     
        ENDPGM 
        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

        Working...
        X