Показать сообщение отдельно
Старый 25.08.2017, 19:11   #1  
Blog bot is offline
Blog bot
Участник
 
25,475 / 846 (79) +++++++
Регистрация: 28.10.2006
stoneridgesoftware: X++ Select Statements That Look More Like SQL
Источник: https://stoneridgesoftware.com/x-sel...more-like-sql/
==============

As I’ve progressed as an AX developer, I’ve had to lean on many of the skills that I had from a former job as a C# developer where I used a lot of SQL queries. Working with data in X++ is similar until you try to write it like it would be written in SQL.

I would like to explain a quick tip that I got when working with multiple joins recently in X++,  as well as some other best practices when working with data.

Working with vendTrans and ProjTable, I needed to join a few tables to get to the data that I needed.  As I stated above, I came from a very SQL query heavy development environment, so my first step when working with data like this is to write it in SQL.



SQL Statement:

select p.Name, p.Workerresponsible, p.custAccount, v.ProjId, vit.*, from PSAPwpInvoiceTransView as v inner join VendInvoiceTrans as vit on vit.RecId = v.VendInvoiceTransRecId inner join VendTrans as vt on vit.InvoiceId = vt.invoice inner join ProjTable as p on p.ProjId = v.projId where vt.voucher = '#########' First X++ select:

select vendTrans join vendInvoiceTrans join ProjId from view join Name, WorkerResponsible, CustAccount from projTable where projTable.ProjId ==view.ProjId && vendTrans.voucher == '#########' && vendInvoiceTrans.InvoiceId == vendTrans.invoice && vendInvoiceTrans.RecId == view.VendInvoiceTransRecId As you can see above, I have all of the same joins and fields selected. There are definite differences in the languages that you cannot get around, but the tip that I received allows you to better see and understand were your joins are and where you might have an issue.

Easier to read X++:

select firstonly vendTrans where vendTrans.voucher == '#########' exists join vendInvoiceTrans where vendInvoiceTrans.InvoiceId == vendTrans.invoice exists join view where vendInvoiceTrans.RecId == view.VendInvoiceTransRecId exists join projTable where projTable.ProjId ==view.ProjId

More X++ Select Statement Tips:

1. Place the where clauses for each join under the line adding the join and table.

This gives you a better view of which fields are being joined, and helps with debugging if you have issues. This also give you a more “SQL-Like” visual of the select statement.

2. Make sure that you are using the correct joins. 

As seen above, I had inner joins on all of the tables and was selecting fields that I thought I needed.  After some testing, I realized the best practice is using Exists Joins, as then I did not need the fields from the joining tables and I only needed to make sure they existed in the table.  This speeds up the select as well as returns only the data that you need from VentTrans.

3. Use field groups to select ONLY the values that you need. 

As seen in the first X++ select that I wrote, I added a few of the fields that I needed from each table. Example:

join Name, WorkerResponsible, CustAccount from projTable This will return only the fields stated, whereas without calling them out you would get all fields from the projTable.

4. First only and First fast. 

In the second select code block, I added the firstOnly directive.  This directive speeds up the data retrieval by getting the first record that it needs. FirstFast was not a great solution to this as it may still incur overhead setting up the cursor to support additional results.  Therefore, if you know there is only a single row, use First only.



Источник: https://stoneridgesoftware.com/x-sel...more-like-sql/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.