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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Why do Microsoft goes with sql statements instead of using x++ select statements

(6) ShareShare
ReportReport
Posted on by 115
Hi experts, 
I came across a method in LedgerJournalTable (Table object) under the method numOfVouchers(). This can be achieved through x++ statements as well. But I just want to understand why Microsoft went with sql statement instead of using X++ statements here. This method is used in posting a journal to check whether there are any journal lines there are to be posted. Any inputs is appreciated. 
 
Regards, 
John
Categories:
I have the same question (0)
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    26,449 Super User 2025 Season 2 on at
     
    because in X++ select statement there's no such thing as a distinct keyword, which is why they use sql statement.
     
    Best regards,
    Mohamed Amine MAHMOUDI
  • Layan Jwei Profile Picture
    8,124 Super User 2025 Season 2 on at
    Hi,

    it would be great if u can share the code of the method, because not all of us have access to our devBoxes when looking at questions:

    But in general based on Mohamed's comment about distinct, then we can still sometimes achieve sth similar to distinct using group by

    for example these two sentences below give the same result
     
    select distinct AccountNum  from custTable
    select AccountNum from custTable group by AccountNum
     
  • John Bright Profile Picture
    141 on at
    Hi Layan, 
    Sorry for the delayed response. Here is the code. 
     
    public Counter numOfVouchers()
        {
            SqlSystem sqlSystem = new SqlSystem();
            SysDictTable journalTransTable = new SysDictTable(tableNum(LedgerJournalTrans));
            FieldName voucherField = journalTransTable.fieldName(fieldNum(LedgerJournalTrans, Voucher));
            FieldName journalNumField = journalTransTable.fieldName(fieldNum(LedgerJournalTrans, JournalNum));
            FieldName dataAreaField = journalTransTable.fieldName(fieldNum(LedgerJournalTrans, DataAreaId));
            FieldName partitionField = journalTransTable.fieldName(fieldNum(LedgerJournalTrans, Partition));
            str sql = 'select count(distinct %1) from %2 where %3 = %4 and %5 = %6 and %7 = %8';
            sql = strFmt(sql,
                         voucherField,
                         journalTransTable.name(),
                         journalNumField,
                         sqlSystem.sqlLiteral(this.JournalNum),
                         dataAreaField,
                         sqlSystem.sqlLiteral(this.DataAreaId),
                         partitionField,
                         getcurrentpartitionrecid());
            SqlStatementExecutePermission permission = new SqlStatementExecutePermission(sql);
            permission.assert();
            //Setup the connection needed to send the command to SQL Server.
            Connection userConnection = new Connection();
            Statement stmt = userConnection.createStatement();
            ResultSet sqlres = stmt.executeQueryWithParameters(sql, SqlParams::create());
            CodeAccessPermission::revertAssert();
            //Select queries record
            sqlres.next();
            return sqlres.getInt(1);
        }
     
    And yes, I agree the same that Mohamed said can be achieved through group by. 
     
    I am just curious why did Microsoft went with an SQL query when the same can be done through x++ select statements. 
  • Deepak Agarwal Profile Picture
    8,602 on at
    This might be related to respective data entity or View maybe. I have seen similar SQL queries written to support view computed fields. 
  • André Arnaud de Calavon Profile Picture
    301,231 Super User 2025 Season 2 on at
    Hi John,
     
    As Mohamed mentioned, it is because of the distinct keyword. A count of records grouped by voucher gives another result than counting the different voucher numbers.
     
    To achieve the same with X++ queries, you would need to create a select statement grouped by the voucher number and then loop the selection to count the number of vouchers. Using the SQL statement with the distinct statement, there is one call and no additional loops. Performance wise the SQL statement wins.
  • Bharani Preetham Peraka Profile Picture
    3,634 Moderator on at
    Agree with you. They may simply create a view do a single select on this and get the count. And the distinct keyword can be added in the View definition itself. I think there is some other reason may be.

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 669 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 449 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 384 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans