查询名称仅在运行时已知的数据库表

时间:2014-05-19 08:29:43

标签: progress-4gl openedge

我在运行时获取数据库表名(让我们假设来自用户)。我需要查询表并返回几个字段(我知道)。怎么做?

“FOR EACH”不会接受变量名称。所以,我无法使用它。

我经历过动态查询,尤其是SET-BUFFERS()方法。即便如此,我还需要知道表名。

我需要类似的东西:

DEF VAR tablename AS CHAR. 
tablename = somename.
FOR EACH tablename WHERE ....:
     ...
     ...
END.

有人可以指出我正确的方向吗?

1 个答案:

答案 0 :(得分:5)

您可以使用动态缓冲区执行动态查询。只需在此示例中替换cTableName变量的值:

/* Replace _file with whatever field you're after */
DEFINE VARIABLE cTableName AS CHARACTER   NO-UNDO INIT "_file".

DEFINE VARIABLE hQuery  AS HANDLE      NO-UNDO.
DEFINE VARIABLE hBuffer AS HANDLE      NO-UNDO.

CREATE BUFFER hBuffer FOR TABLE cTableName.
CREATE QUERY hQuery.

hQuery:SET-BUFFERS(hBuffer).

hQuery:QUERY-PREPARE("FOR EACH " + cTableName).
hQuery:QUERY-OPEN().

REPEAT:
    hQuery:GET-NEXT().
    IF hQuery:QUERY-OFF-END THEN LEAVE.

    DISPLAY hBuffer:BUFFER-FIELD(1):BUFFER-VALUE.

    /* If you know the name of the field you can do: */
    /* DISPLAY hBuffer:BUFFER-FIELD("nameoffield"):BUFFER-VALUE. */

END.


/* Clean up */
hQuery:QUERY-CLOSE().
hBuffer:BUFFER-RELEASE().
DELETE OBJECT hBuffer.
DELETE OBJECT hQuery.