【问题标题】:How to give an error message if none of the records are matching in FOR EACH loop?如果 FOR EACH 循环中没有任何记录匹配,如何给出错误消息?
【发布时间】:2020-01-16 17:15:15
【问题描述】:

我编写了一个程序,用于在 FOR Each 循环中将一条记录与另一条记录进行匹配,但如果没有一条记录匹配,我不知道如何给出错误消息。让我分享我的代码

DEFINE VARIABLE cPos AS INTEGER NO-UNDO.
DEFINE TEMP-TABLE tt_data NO-UNDO
      FIELD cPosition AS CHARACTER FORMAT "X(60)"
      FIELD cEndCode  AS CHARACTER
      FIELD cShotCode AS CHARACTER.
/*so many records are stored in tt_data and below is one of the records for your understanding*/

CREATE tt_data.
ASSIGN
tt_data.cPosition ="S$$$^^^^^^^^^^$$$^^^MC^^^^^^^^^^^^R^^^^^^^^^^^^^^^^^^^^^^^^^"
tt_data.cEndCode =  10
tt_data.cShotCode = "S".

cPos = integer( tt_data.cEndCode / 10 ).

/* Consider 60 records available in tt_data */
FOR EACH tt_data.

     FIND FIRST tt_date WHERE tt_data.cShotCode =  
     SUBSTRING(tt_data.cPosition,cPos,1) NO-LOCK NO-ERROR. 

     DISPLAY tt_data.cShotCode. /* Displayed Value is S */

IF NOT AVAILABLE tt_date THEN
    MESSAGE "NONE OF THE RECORDS MATCHING WITH tt_data.cPosition "
    LEAVE.
END.

我可以在 tt_data 中获得至少一条记录匹配。这里的问题是,如果任何一条记录匹配,我不想离开,但如果没有记录匹配,我想用 LEAVE 语句收到错误消息。你能帮帮我吗?

【问题讨论】:

    标签: openedge progress-4gl


    【解决方案1】:

    我认为这可能是你想要做的:

    DEFINE VARIABLE cPos AS INTEGER NO-UNDO.
    DEFINE TEMP-TABLE tt_data NO-UNDO
          FIELD cPosition AS CHARACTER FORMAT "X(60)"
          FIELD cEndCode  AS CHARACTER
          FIELD cShotCode AS CHARACTER.
    
    /*so many records are stored in tt_data and below is one of the records for your understanding*/
    
    CREATE tt_data.
    ASSIGN
    tt_data.cPosition ="S$$$^^^^^^^^^^$$$^^^MC^^^^^^^^^^^^R^^^^^^^^^^^^^^^^^^^^^^^^^"
    tt_data.cEndCode =  10
    tt_data.cShotCode = "S".
    
    cPos = integer( tt_data.cEndCode / 10 ).
    
    /* Consider 60 records available in tt_data */
    
    FOR EACH tt_data:  /* although it 'works', "." is not appropriate, FOR EACH should end with a ":" */
    
        FIND FIRST tt_date WHERE tt_data.cShotCode = SUBSTRING(tt_data.cPosition,cPos,1) NO-LOCK NO-ERROR. 
    
        /* changes start here */
    
        IF AVAILABLE tt_date THEN
          do:
            DISPLAY tt_data.cShotCode. /* Displayed Value is S */ /* only display this when it is available! */
          end.
         else
          do:
            MESSAGE "NONE OF THE RECORDS MATCHING WITH tt_data.cPosition ".  /* a "." was missing */
            /* LEAVE. */ 
          end.
    
    END.
    

    【讨论】:

      【解决方案2】:

      我会亲自尝试在您进入 FOR EACH 块之前进行错误检查。有时您不能,但根据您的示例代码,我认为您可以先检查临时表并提供错误消息。但是根据示例,我不完全确定您要做什么。

      DEFINE VARIABLE cPos AS INTEGER NO-UNDO.
      DEFINE TEMP-TABLE tt_data NO-UNDO
            FIELD cPosition AS CHARACTER FORMAT "X(60)"
            FIELD cEndCode  AS CHARACTER
            FIELD cShotCode AS CHARACTER.
      /*so many records are stored in tt_data and below is one of the records for your understanding*/
      
      CREATE tt_data.
      ASSIGN
      tt_data.cPosition ="S$$$^^^^^^^^^^$$$^^^MC^^^^^^^^^^^^R^^^^^^^^^^^^^^^^^^^^^^^^^"
      tt_data.cEndCode =  10
      tt_data.cShotCode = "S".
      
      cPos = integer( tt_data.cEndCode / 10 ).
      
      
      /* Add check here */
      IF can-find( FIRST tt_date WHERE 
                         tt_data.cShotCode = SUBSTRING(tt_data.cPosition,cPos,1 ) )
      THEN
        message "ERROR message".
      
      ELSE DO:
        /* Consider 60 records available in tt_data */
        FOR EACH tt_data.
          /* DO thing */
        END.
      END.
      

      【讨论】:

        猜你喜欢
        • 2020-09-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多