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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Extending a QueryObject with a NOT EXISTS clause

(0) ShareShare
ReportReport
Posted on by 329

I have a batch where I receive a query object, which I want to extend.

I do this with the following code:

qbds2 = query.dataSourceTable(tablenum(TMSTransportationTender)).addDataSource(tablenum(PurchTable));
qbds2.joinMode(JoinMode::NoExistsJoin);
qbds2.addLink(fieldnum(TMSTransportationTender, RefId), fieldnum(PurchTable, TenderId));
qbds2.addRange(fieldnum(PurchTable, TenderId)).value(enum2value(NoYes::No));
QueryRun queryRun = new QueryRun(query);

When afterwards I inspect the QueryRun object, I can see the following Diry SQL:

SELECT CarrierCode, LoadId, RefId, RequestedRate, RequestedPickupDateTime
FROM TMSTransportationTender(TMSTransportationTender)
WHERE ((TenderStatus = 4)) AND ((TenderRefType = 0))
JOIN *
FROM WHSLoadTableLatestTransportationTenderView(WHSLoadTableLatestTransportationTenderView) ON TMSTransportationTender.TransportationOrderNum = WHSLoadTableLatestTransportationTenderView.LastTransportationOrderNum
JOIN AccountNum, LoadId
FROM WHSLoadTable(whsLoadTable) ON TMSTransportationTender.RefId = WHSLoadTable.LoadId	AND ((LoadStatus = 6))
WHERE ((TMSTransportationTender(TMSTransportationTender).RefId = N'NL0001951')) 
NOTEXISTS JOIN *
FROM PurchTable(PurchTable_1)
WHERE TMSTransportationTender.RefId = PurchTable.TenderId
	AND ((TenderId = N'No'))

I can see that it seems that the NOT EXISTS clause is actually added to SQL, but when I continue I receive a result which I should NOT receive.

The row with RefId 'NL0001951' (the only one returned since I search for it via the batch parameters) should NOT be returned, as there are multiple Purchase Orders in the database referring to this RefId.

Pasting the Dirty SQL query to normal SQL also shows that query returning no data, as expected.

Does anyone know whether Im missing something or why this is not working?

Expected real SQL:

SELECT CarrierCode, LoadId, RefId, RequestedRate, RequestedPickupDateTime
FROM TMSTransportationTender t
INNER JOIN WHSLoadTableLatestTransportationTenderView ON t.TRANSPORTATIONORDERNUM = WHSLoadTableLatestTransportationTenderView.LastTransportationOrderNum
INNER JOIN WHSLoadTable ON t.RefId = WHSLoadTable.LoadId
WHERE ((TenderStatus = 4))	AND ((TenderRefType = 0)) and (t.RefId = N'NL0001951') AND ((LoadStatus = 6)) AND 
NOT EXISTS(SELECT TENDERID FROM PurchTable p WHERE p.TENDERID = t.REFID)

I have the same question (0)
  • Verified answer
    Vignesh.A Profile Picture
    203 on at

    Hi Superbunny,

    Is the expectation is to get records from table "TMSTransportationTender" which has no purchase orders linked to it.

    If that is the scenario, Your not exists join is correct.

    But why have you added a range on the field "TenderId"  of Purchtable. It is not required i believe. Even if it required for some reason, The "TenderId" field is a string field and the the value added for the range is enum.

    Removing the range should fetch you the expected records

  • Superbunny Profile Picture
    329 on at

    Yes, that is indeed the expectation. I thought the range part was used to supply the column which would be checked for NULL values for the exists join (like in my normal SQL variant).

    Thank you, I will check this out.

  • Suggested answer
    Vignesh.A Profile Picture
    203 on at

    Hi Superbunny,

    Is the issue resolved. If yes please mark it as verified

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 577 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 309

#3
Diego Mancassola Profile Picture

Diego Mancassola 259

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans