Search code examples
indexingaxaptadynamics-ax-2012

Dynamics AX 2012 Unique Index Allow Nulls


In ANSI SQL, you can create a unique Index with a WHERE clause that will ignore "" values.

Is there a way to do this in Dynamics AX?

Add a unique index to a table that only cares about rows where the indexed column contains data?


Solution

  • I don't think this is possible because Dynamics AX does not allow null values:

    Microsoft Dynamics AX does not support the concept of null values that is available in many other Database Management Systems (DBMS). A field in Microsoft Dynamics AX always has a type and a value.

    - Null Values for Data Types [AX 2012], MSDN

    Update

    I don't think it is possible to set up a "selective" index in Dynamics AX that only works for some values but ignores others. To do that, you would have to set up the index directly in the SQL database. Of course the data dictionary synchronisation would delete such a index. You probably could write a sql procedure that creates the index and customize the code that executes the synchronisation to call the procedure after every synchronisation. I would advise against such a solution, though, because it is error prone and circumvents the data dictionary synchronisation, which is one of the core mechanics of Dynamics AX.

    An alternative could be to separate your field with the '' values into a separate table and only create records in that table if you have an actual value for the field. Then outer join the new table with the existing table.

    Yet another way could be to not use an index, but program a data validation that checks if the non empty value in the field already exists.