Literals in Ax

Hi Everyone,

I do understand what literals is.

Can you please tell what this number 2 indicates inside brackets of literals.

I also see other numbers like literals(0) etc.,

Code copied from Inventory onhand:

inventDim_DS.query().literals(2);

Thanks

Can you clarify whether you mean literals in X++ or literals() method of Query class (which is related to database queries and has nothing to do with literals in X++ language)?

Hi Martin, Thanks for the reply.

This is for Query class. Could you also tell what is the differnce between literals in X++ and query.

I don’t know if you’re aware of things like execution plans in SQL Server, so let me give you a very simplified explanation.

When you run a query, SQL Server takes a look at it and decide what’s the best way to run it. Because this takes time, it will cache this decision and do it in the same way for similar queries.

Now imagine that you have a parameter in WHERE, such as WHERE ID=1. Usually you want to ignore the actual value and want to execute the query in the same way for ID=2, for instance.

But in some complicated scenarios, you may want to force the database server to use different way for different values and therefore you tell AX to reveal the actual values. The database will then create a new plan for every value, which means that it needs to do more work before execution and it must cache more things, so you should do it only if there is a clear benefit confirmed by testing.

Use can use Query.literals(true) or forceLiterals when writing select statements to do it, but note that it’s not recommended and you should be very careful with it. Most AX developers never use it.

For the other meaning, please look at Wikipedia: Literal (computer programming).

Appreciate your answer. Thank-you.