Help generating dynamic for each

mcknight

New Member
I'm fairly new to progress and I need a bit of help generating a for each statement that I can dynamically change.
this is what I have so far:

<clip>
define var as int.
var = 10.

for each record where record.id = var no-lock break by record.second-id:
more code
</clip>

instead of looking for just "10" in this case I want to build the for each statement to use 'var' for the entire where block. I added a second var with an or and it worked fine, like:
"for each record where record.id = var or record.id = var2 no-lock break by record.second-id:"

but when I try and put both in a single variable it breaks.

<clip>
define var as char.
var = "record.id = 10 or record.id = 11".

for each record where var no-lock break by record.second-id:
more code
</clip>


any ideas on how to get it to work or if I should try another way.

thanks for the help?
 

mcknight

New Member
gordon we are using 9.1C17

simon:
I get what your saying about using the query-prepare and I'm going to try that right now.
One question I still have is why does it allow me (citing the original code) to dynamically add single variables to the for each statement, like 'var' and 'var2', but won't allow me to add a character string with the column name and criteria already included?
Is this a constraint in progress itself and if it is why hasn't it been updated?
 

gcampbell

Member
This is a constraint of Progress. It isn't possible to do what you want with the FOR EACH statement. It is possible with dynamic queries (as Simon pointed out).

The Temp-Table Expositor (available on www.peg.com) really illustrates the use of dynamic temp-tables and queries.
 

mcknight

New Member
why hasn't this been added into newer versions of progress?
it seems like a fairly useful and relatively easy function to add. I would have to say that in all the other programming languages i've used (c, java, web scripting) you can concatenate char strings to build dynamic loops or queries.

since no one here controls the development of progress i guess not many people will be able to have an answer for this.
 
Dynamic queries seem a little complex initially, but they are quite powerfull.

You can also use a standard for each statement in a number of different ways to achieve a fair amount of flexability. Here are some examples, but using dynamic queries is usually more efficient.

Code:
def var vIDlist as char init "10,11" no-undo.
 
for each record where can-do(vIDlist, string(record.id)):
end.

Code:
def var vIDOne as int no-undo.
def var vIDTwo as int no-undo.
 
assign
	vIDOne = 11
	vIDTwo = ?.
 
for each record where 
	(vIDOne = ? or record.id = vIDOne) and
	(vIDTwo = ? or record.id = vIDTwo):
end.

Code:
def temp-table ttAllowIDs no-undo
	field id like record.id.
 
create ttAllowIDs. ttAllowIDS.id = 11.
create ttAllowIDs. ttAllowIDS.id = 12.
 
for each record where
	can-find(first ttAllowIDs where ttAllowIDS.id = record.id):
end.
 

mcknight

New Member
thanks simon, i'll droping in that code now.
I had followed the information on the link you had posted, there were a couple of minor changes but I was still having some small questions as to how to loop through the records. I'll let you know how it goes.
 
Top