Monday, March 22, 2010

The firstonly trap

Using the firstonly qualifier on a X++ Select Statement can increase speed performance. So why don't use it for update_recordset and delete_from too? This was my Idea and I was going on to rewrite this code:

ttsbegin;
select firstonly forupdate myTable where myTable.NotUniqueField == 'anyValue';
myTable.delete();
ttscommit;

into that

delete_from firstonly myTable where myTable.NotUniqueField == 'anyValue';

Unfortunately, the result was not as expected: All records which matched the condition were lost. First I thought the problem was, that a delete_from firstonly has the same inaccuracy as a select firstonly and therefore it wouldn't be granted to proceed only one record. But actually it seems that the firstonly qualifier is neither implemented for update_recordset nor for delete_from (even the X++ compiler accept it).

On the other hand, T-SQL provides the TOP clause in select, update and delete statement too - and why the compiler should swallow it, even it makes no sense? I could imagine, that this features will be implemented in later versions. I will use the firstonly qualifier again, but only if the where conditions refers a unique record.

delete_from firstonly myTable where myTable.UniqueField == 'keyValue';
// and
update_recordset firstonly myTable setting AnyField = 'anyValue' where myTable.UniqueField == 'keyValue';

May be, code from today is still active in later AX versions, when update_recordset and delete_from are supporting the firstonly qualifier. Further I like it as a hint when reading and try to understand source code.

So be warned to fall into that trap :)

1 comment: