firstOnly returns more than 1 record when using While select, so what's the desired way to do the statement?

Hello All,

I just found the following statement doesn’t work, as the “firstOnly” part is returning more records than 1, as I debugged.

===X++ select statement=========

while select field1, field2
from Table_A
join * from Table_B
where condition_002 between Table_A and Table_B (table_A to Table_B is 1 to 1 by foreigh key condition)
join firstOnly * from Table_C
where condition_003 between Table_B and Table_C (table_B and table_C could potentially be 1 to Many, but the condition only wants to get the first record, as we don’t want to expend the whole data set size.)

==============

After checked the MSDN documents at https://msdn.microsoft.com/en-us/library/aa661012.aspx, it turns out "The firstOnly keyword does not guarantee that a maximum of one record is retrieved by an X++ SQL select statement. " And, at https://msdn.microsoft.com/en-us/library/aa861766.aspx, at “Using the firstOnly Qualifier” section, it says The firstOnly qualifier does not guarantee that only one record is returned. For more information, Do not use while select firstOnly.

OK, so go back to the X++ select statement, what should I change, if the original idea to get ONLY 1 from table_C, can be realized?

Thank you,

Kwen

Firstonly isn’t supported on joins. You’ll have to implement your requirement differently.

For example, you may be able to filter your data in some way. You may use a view with computed column, as I explained in Join first line in AX 2012. You may want to get the first record of Table_C inside the loop. And so on.

Thank you Martin!