2

Good day:

Quick question: Can I perform a dynamic query in OpenEdge?

Example:

def temp-table tt-num1
field f1 as int
field f2 as int.

def temp-table tt-num2
field f1 as int
field f2 as int.

def temp-table tt-num3
field f1 as int
field f2 as int.

What I need is something that looks like this:

procedure repeat-query:
for each 'variable that contains table-name' no-lock.

disp f1 f2.

end.
end procedure.

or some other way that can solve my problem.

How do I proceed with this? I tried to check for dynamic query on the Internet but with no luck. Thanks in advance.

jhrabi
  • 309
  • 2
  • 16
noob
  • 165
  • 3
  • 18

1 Answers1

5

If you go directly to https://documentation.progress.com/#page/progdocindex%2Fopenedge.html you can find documentation around everything OpenEdge. For instance dynamic queries.

I don't understand exactly what you try to do but here's an example of a dynamic query.

DEFINE TEMP-TABLE tt-num1 NO-UNDO
    FIELD f1 AS INTEGER
    FIELD f2 AS INTEGER.

DEFINE TEMP-TABLE tt-num2 NO-UNDO
    FIELD f1 AS INTEGER
    FIELD f2 AS INTEGER.

DEFINE TEMP-TABLE tt-num3 NO-UNDO
    FIELD f1 AS INTEGER
    FIELD f2 AS INTEGER.


CREATE tt-num1.
ASSIGN 
    tt-num1.f1 = 1
    tt-num1.f2 = 1.

CREATE tt-num1.
ASSIGN 
    tt-num1.f1 = 1
    tt-num1.f2 = 2.

CREATE tt-num1.
ASSIGN 
    tt-num1.f1 = 2
    tt-num1.f2 = 1.

CREATE tt-num1.
ASSIGN 
    tt-num1.f1 = 2
    tt-num1.f2 = 2.

DEFINE VARIABLE hQuery  AS HANDLE      NO-UNDO.
DEFINE VARIABLE cBuffer AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cField  AS CHARACTER   NO-UNDO.
DEFINE VARIABLE iValue  AS INTEGER     NO-UNDO.

ASSIGN 
    cBuffer = "tt-num1"
    cField  = "f1"
    iValue  = 1.

CREATE QUERY hQuery.

hQuery:ADD-BUFFER(cBuffer).
hQuery:QUERY-PREPARE("for each " + cBuffer + " where " + cBuffer + "." + cField + " = " + STRING(iValue)).
hQuery:QUERY-OPEN().

queryLoop:
REPEAT:
    hQuery:GET-NEXT().

    IF hQuery:QUERY-OFF-END THEN LEAVE queryLoop.

    DISPLAY hQuery:GET-BUFFER-HANDLE(1):BUFFER-FIELD(cField):BUFFER-VALUE.
END.
hQuery:QUERY-CLOSE().

DELETE OBJECT hQuery.

As Stefan Drissen mentions in a very valid comment: the loop can be more compact:

DO WHILE hQuery:GET-NEXT():
    /* Code goes here */
END.
Jensd
  • 7,886
  • 2
  • 28
  • 37
  • 3
    I prefer making the get-next part of the while - keeps things nice and clean, no need for query-off-end. repeat while hQuery:get-next(): display ... end. – Stefan Drissen Aug 23 '17 at 22:26
  • @StefanDrissen can i use dynamic query to create record? like CREATE tt-num1, can i do this with buffer? – noob Aug 24 '17 at 01:00