web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Executing direct SQL Statements

(0) ShareShare
ReportReport
Posted on by

Hey...

are there any reasons why I should NOT use direct SQL statements? I mean, sometimes I just want to access one simple field of a table and the "normal" statement selects every field of a table. I could reduce the network load due to returning the single field instead of all columns of a table. Or does it make no sense due to some other reasons?!

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Vilmos Kintera Profile Picture
    46,149 on at
    RE: Executing direct SQL Statements

    Using direct SQL statements is a horrible idea.

    Your concerns on network packets for larger cursor size and roundtrips from SQL are valid, that is exactly why you would get a Best Practice deviation error if you enable the AX best practice checks for Errors and Warnings in the Compiler settings, and compile your code for a select * from table; line.

    Generally, you would always want to avoid an asterisk select or full joins between tables - always use exists/notexists joins for multiple datasources for filtering purposes, and provide a field list for selects and inner joins on the data that you are actually working with.

    You would only get the values from the selected fields back, nothing else.

    One very important difference between a typical T-SQL statement and AX statements translated into SQL is that AX kernel makes parametrized versions of the query, meaning when you execute the same query twice, SQL Server will already have that Query Execution Plan available even if you provide different filtering criteria in the where clause, so it does not have to compute a new query plan. Parametrization of SQL statements is an excellent performance booster, which you would not typically write for a regular qucik data pull in T-SQL statements.

  • Verified answer
    guk1964 Profile Picture
    10,886 on at
    RE: Executing direct SQL Statements

    Generally better to query a view of the table than to query direct.

    Many of the oob reports are in SSRS and the underlying queries for those can be used- you will often  find that what you need is already brought into  the rdp layer.

    When you query a table – even WITH (NOLOCK) – you take out a schema stability lock.  No one else can change that table or its  indexes until your query is finished.  That isn't a big deal until you need to drop an index, but it is when you can’t because people are constantly querying a table, and they think there’s no overhead as long as they use WITH (NOLOCK).

    Read about– one reason why READ COMMITTED SNAPSHOT ISOLATION is the best option of Ax. .  It gets you consistent data with less blocking hassles.

  • Mea_ Profile Picture
    60,284 on at
    RE: Executing direct SQL Statements

    No, it will give you empty value. Easiest way to check is to run SQL server profiler and check SQL statements created by AX.

  • Community Member Profile Picture
    on at
    RE: Executing direct SQL Statements

    SalesLine saleLine;

    select itemId, inventDimId from salesLine;

    Are you sure, that the statement will just retrieve the two fields "itemID" and "inventDimId" instead of all of them?

    Info(salesline.otherfield);

    will probably also work right?

  • Verified answer
    Mea_ Profile Picture
    60,284 on at
    RE: Executing direct SQL Statements

    Hey PeterDetzner,

    That is a good list of reasons why not to do direct SQL dev.goshoom.net/.../why-not-to-touch-ax-db-directly

    You can specify fields you want to select using x++ as well, just do

    select itemId, inventDimId from salesLine;

    instead of

    select salesline;

    Please note that you can have different behavior in AX related to caching, so for example, if you have EntireTable cache AX selects all the records from table to cache them irrespective of fields specified but it's done to increase performance.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Guy Terry Profile Picture

Guy Terry 2 Moderator

#1
Martin Dráb Profile Picture

Martin Dráb 2 Most Valuable Professional

#1
Community Member Profile Picture

Community Member 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans