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

**URL:** <https://www.dynamicsuser.net/t/firstonly-returns-more-than-1-record-when-using-while-select-so-whats-the-desired-way-to-do-the-statement/60445>\
**Category:** Developer Forum\
**Created:** [April 19, 2016, 6:09pm UTC](https://www.dynamicsuser.net/t/firstonly-returns-more-than-1-record-when-using-while-select-so-whats-the-desired-way-to-do-the-statement/60445 "2016-04-19T18:09:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lkustb](https://avatars.discourse-cdn.com/v4/letter/l/34f0e0/32.png) [@lkustb](https://www.dynamicsuser.net/u/lkustb)\
**Post date:** [April 19, 2016, 6:09pm UTC](https://www.dynamicsuser.net/t/firstonly-returns-more-than-1-record-when-using-while-select-so-whats-the-desired-way-to-do-the-statement/60445/1 "2016-04-19T18:09:37Z")

</div>

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](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](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

---

<div class="post-metadata">

**Author:** ![MartinDrab](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@MartinDrab](https://www.dynamicsuser.net/u/MartinDrab)\
**Post date:** [April 19, 2016, 7:46pm UTC](https://www.dynamicsuser.net/t/firstonly-returns-more-than-1-record-when-using-while-select-so-whats-the-desired-way-to-do-the-statement/60445/2 "2016-04-19T19:46:14Z")

</div>

_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](http://dev.goshoom.net/en/2015/06/join-first-line-in-ax-2012/). You may want to get the first record of _Table\_C_ inside the loop. And so on.

---

<div class="post-metadata">

**Author:** ![lkustb](https://avatars.discourse-cdn.com/v4/letter/l/34f0e0/32.png) [@lkustb](https://www.dynamicsuser.net/u/lkustb)\
**Post date:** [April 21, 2016, 8:19pm UTC](https://www.dynamicsuser.net/t/firstonly-returns-more-than-1-record-when-using-while-select-so-whats-the-desired-way-to-do-the-statement/60445/3 "2016-04-21T20:19:41Z")

</div>

Thank you Martin!
