I'm writing an SQR program to send a vendor a file containing employee info. The file contains a number of fields for which I've assigned the variables $Code_1 $Code_2 $Code_3 ....
Each code has an associated rate, and I've assigned similar variables ($Rate_1, $Rate_2, etc...)
I have a lookup table that has the columns EMPLID, JOBCODE, HOURLY_RT. I need to loop through for each employee to get all of the codes/rates. It's possible that some employees will have more/fewer than others. Is it possible to have "dynamic" variables, like we do for dynamic sql? For example, something like $Code_[$i]? The thought was to do something like this:
let #i = 1
begin-select
EC.JOBCODE
EC.HOURLY_RT
let $Code_[$i] = &EC.JOBCODE
let $Rate_[$i] = &EC.HOURLY_RT
let #i = #i + 1
FROM PS_ACME_LOOKUP EC
WHERE EC.EMPLID = &J.EMPLID
end-select
This doesn't work, but I wondering if there's a similar (or better) way to accomplish this. I suppose I could do an evaluate of the counter: when #i = 1, $Code_1 = ... when #i=2, $Code_2 =... But I'm hoping there's a better way.
Thanks
Edit - Just for added clarification, for each employee, a single line will be written to a file, with the fields for each of these values (populated or not) - so the line will have: $EMPLID $Code_1 $Code_2 $Code_3.....$Rate_1 $Rate_2 $Rate_3
For further clarification the lookup table will have multiple rows for each employee, so the table might look like this:
EMPLID JOBCODE HOURLY_RT
0001 ABC 10.50
0001 DEF 9.75
0001 GHI 9.50
When I populate the variables, looping through the table, I would want $Code_1 = 'ABC', $Rate_1 = 10.50, $Code_2 = 'DEF', Rate_2 = 9.75 etc...
You can use arrays in SQR.
To set up the array:
Create-Array Name=WorkArray Size = 100
Field=Code
Field=Rate
Let #NumCodesForEmp = 0
To add data in your Select Block - also use on-break before and after procedures:
Begin-Select
EC.Emplid () on-break print=never before=Init-Emp After=Process-Emp
Let $Emplid = &EC.Emplid
add 1 to #NumCodesForEmp
Put &EC.JobCode &EC.Rate into WorkArray(#NumCodesForEmp) Code Rate
Write the before procedure to initialize:
Begin-Procedure Init-Emp
Let #NumCodesForEmp = 0
End-Procedure
When done with the employee:
Begin-Procedure Process-Emp
Let #I = 1
Let $OutputLine = $Emplid
While #I <= #NumCodesForEmp
Get $Code $Rate From WorkArray(#I) Code Rate
Let $OutputLine = $Outputline || ',' || $Code || ',' || $Rate
add 1 to #I
End-While
! This assumes that file number 10 is open
Write #10 from $OutputLine
End-Procedure
However, I think you could do everything without an array - use the before and after procedures as so:
Begin-Procedure Init-Emp
Let $OutputLine = &EC.Emplid
End-Procedure
Begin-Procedure Process-Emp
Write #10 from $OutputLine
End-Procedure
Then the Select Block would look like this:
Begin-Select
EC.Emplid () on-break print=never before=Init-Emp After=Process-Emp
EC.JobCode
EC.Rate
Let $OutputLine = $OutputLine || ',' || &EC.Jobcode || ',' || &EC.Rate
When using on-break, make sure you sort by emplid. This is much simpler if your need is just to write a file from data from a table.