如何在 FOR EACH 查询中检查同一元素的多个子字符串?

How can I check multiple substrings for the same element in a FOR EACH query?

我正在尝试读取字符串的一部分,但编译器只读取第一个子字符串。我的代码如下:

FOR EACH estrutura NO-LOCK,
EACH item
WHERE estrutura.it-codigo = item.it-codigo
AND IF pc-ge <> 0           THEN item.ge-codigo = pc-ge ELSE item.ge-codigo > 0
AND IF pc-familia <> ""     THEN SUBSTRING(estrutura.it-codigo, 1, 2, "CHARACTER") = pc-familia ELSE SUBSTRING(estrutura.it-codigo, 1, 2, "CHARACTER") BEGINS pc-familia
AND IF pc-norma <> ""       THEN SUBSTRING(estrutura.it-codigo, 3, 1, "CHARACTER") = pc-norma ELSE SUBSTRING(estrutura.it-codigo, 3, 1, "CHARACTER") BEGINS pc-norma
AND IF pc-classe <> ""      THEN SUBSTRING(estrutura.it-codigo, 4, 2, "CHARACTER") = pc-classe ELSE SUBSTRING(estrutura.it-codigo, 4, 2, "CHARACTER") BEGINS pc-classe
AND IF pc-bitola <> ""      THEN SUBSTRING(estrutura.it-codigo, 6, 2, "CHARACTER") = pc-bitola ELSE SUBSTRING(estrutura.it-codigo, 6, 2, "CHARACTER") BEGINS pc-bitola
AND IF pc-comprim <> ""     THEN SUBSTRING(estrutura.it-codigo, 8, 3, "CHARACTER") = pc-comprim ELSE SUBSTRING(estrutura.it-codigo, 8, 3, "CHARACTER") BEGINS pc-comprim
AND IF pc-rosca <> ""       THEN SUBSTRING(estrutura.it-codigo, 11, 2, "CHARACTER") = pc-rosca ELSE SUBSTRING(estrutura.it-codigo, 11, 2, "CHARACTER") BEGINS pc-rosca
AND IF pc-acab <> ""        THEN SUBSTRING(estrutura.it-codigo, 13, 2, "CHARACTER") = pc-acab ELSE SUBSTRING(estrutura.it-codigo, 13, 2, "CHARACTER") BEGINS pc-acab
AND IF pc-seq <> ""         THEN SUBSTRING(estrutura.it-codigo, 15, 2, "CHARACTER") = pc-seq ELSE SUBSTRING(estrutura.it-codigo, 15, 2, "CHARACTER") BEGINS pc-seq NO-LOCK
BY estrutura.it-codigo:

    CREATE tt-estrutura.
    ASSIGN tt-estrutura.nivel-00 = estrutura.it-codigo
    .


END.

table 和变量是之前创建的。感谢阅读,抱歉,如果我没有说清楚。

您不应尝试使用一个大的静态 WHERE 子句来执行此操作。而是创建一个特定于 运行 时间可用的数据的动态查询。像这样:

define variable wc as character no-undo.
define variable b  as handle    no-undo.
define variable q  as handle    no-undo.

wc = "for each customer no-lock where discount > 0".  /* this is the magic part */

create buffer b for table "customer".
create query  q.

q:set-buffers( b ).
q:query-prepare( wc ).
q:query-open.

do while q:get-next( no-lock ):

  display b:buffer-field( "name" ):buffer-value format "x(30)" with 10 down.
  down.

end.

q:query-close.

(我没有你的数据库,所以我用“体育”数据库做一个例子。)

这里的重点是,与其尝试构建一个 WHERE 子句来在编译时处理所有可以想象到的可能性,不如将其推迟到 运行 时间并使用“wc”字符串变量仅包含选择您实际需要并在 运行 时间可用的标准。

因此,除了那些 ANDed IF 语句之外,您还可以这样说:

wc = "for each item no-lock where ".

if pc-ge <> 0 then
  wc = wc + "item.ge-codigo = pc-ge ".
 else
  wc = wc + "item.ge-codigo = pc-ge ".

依此类推,直到您针对具体情况构建了正确的 where 子句,没有任何额外内容。

(动态查询可以包含连接 - 但在您展示的情况下,使用嵌套查询可能更简单,将“FOR EACH estrutura”保留为外部查询。