-
-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unsupported CTE clause: WITH #292
Comments
It's something I've considered, but haven't seen much demand for yet. Could you share an example of a query you're wanting to use CTEs in? |
Hi, I have the same request. Here the simpliest CTE :
Result is :
But the original CTE I wanted to do was :
The goal of this query was to be able to find if a connectionreference is used in more than 1 solution, to check dependencies... Thanks for advance. |
In this case you can rewrite the query using a subquery instead of a CTE: select cte.connectionreferenceid,count( distinct s1.friendlyname)
from (
SELECT distinct cr.connectionreferenceid
FROM connectionreference cr
JOIN solutioncomponent sc1 ON sc1.objectid = cr.connectionreferenceid
JOIN solution s1 ON s1.solutionid = sc1.solutionid
WHERE s1.friendlyname LIKE 'MyCompany%'
) cte
join dependency D on cte.connectionreferenceid=D.requiredcomponentobjectid
join workflow W on W.workflowid=D.dependentcomponentobjectid
JOIN solutioncomponent sc1 ON sc1.objectid = W.workflowid
JOIN solution s1 ON s1.solutionid = sc1.solutionid
where s1.friendlyname != 'Solution par défaut' and s1.friendlyname != 'Solution active'
group by cte.connectionreferenceid |
This may or may not be a valid reason for developing CTE functionality: I have a three level hierarchy of territories. Each user belonging to a territory is able to access records associated with their territory and all territories below in the hierarchy. To see which territories users have access to, I imagined that something like this would do the trick:
Thanks for the work you do! |
I am still planning to add CTE support, I’m hoping to start this in the next month or so. For a query like yours where you’ve got a fixed number of hierarchy levels you’re looking at you can rewrite this with subqueries - in fact this is how SQL Sever would execute the query by essentially copying the CTE definition into each place you’re referring to it in the main query. Although using a CTE instead of subqueries makes this much more readable, the real power of a CTE comes when you want to query a variable number of levels in a hierarchy using recursive CTEs. I’m hoping to be able to support this over the next few updates. |
Hi Mark, I have simple example in mind where CTEs are irreplaceable is navigating hierarchical data, like location levels in Dynamics CRM. let’s consider a Locations table with LocationID, ParentLocationID, LocationLevel, and Country. WITH LocationHierarchy AS (
SELECT LocationID, ParentLocationID, LocationLevel, 0 AS Depth
FROM Locations WHERE LocationID = 'Root-Location-ID'
UNION ALL
SELECT l.LocationID, l.ParentLocationID, l.LocationLevel, lh.Depth + 1
FROM Locations l INNER JOIN LocationHierarchy lh ON l.ParentLocationID = lh.LocationID
)
SELECT Depth, LocationLevel FROM LocationHierarchy ORDER BY Depth; I think this is difficult to achieve without CTEs and would be really a game-changer for dealing with hierarchical data in Dynamics. what do you think? |
Absolutely, this is exactly why it's needed. I've made a start, it won't be in the next update but hopefully another month or so. |
First of all, I would like to express my appreciation for your work on the SQL4CDS plugin for XRMToolBox. It has been an incredibly useful tool for querying Dynamics 365 data.
I'm running into issues using Common Table Expressions (CTEs) in SQL queries due to the lack of support in SQL4CDS. Could you please consider adding support for CTEs in a future release of SQL4CDS?
The text was updated successfully, but these errors were encountered: