我编写了一个程序,用于在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语句得到一条错误消息。您能帮忙这个案例吗?
我认为这可能是您要尝试做的事情:
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.
在您进入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.