如果在FOR EACH循环中没有记录匹配,如何给出错误消息?

问题描述 投票:0回答: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 ).

/* 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
2个回答
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.

0
投票

在你进入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.
© www.soinside.com 2019 - 2024. All rights reserved.