MajP
You've got your good things, and you've got mine.
- Local time
- Today, 12:44
- Joined
- May 21, 2018
- Messages
- 9,493
As mentioned here
I am in the process of converting a complex Access database over to SQL Server in order to teach myself. As mentioned this is a self referencing hierarchical database dealing with a population of whales. I am doing my first Recursive CTE and works well. I can return the descendants for a given mother whale.
1 ALY NULL ALY
2 AME ALY ALY
2 250 ALY ALY
2 AY9 ALY ALY
3 AME04 AME ALY
3 AME08 AME ALY
3 AME12 AME ALY
3 AME16 AME ALY
3 ANH AME ALY
3 405 AME ALY
3 ANN AME ALY
3 579 AME ALY
3 AVA AME ALY
4 AVA13 AVA ALY
4 AVA19 AVA ALY
4 ALF ANN ALY
4 ANN08 ANN ALY
4 ANN12 ANN ALY
4 ANN17 ANN ALY
4 ANH02 ANH ALY
4 ANH07 ANH ALY
4 AKI ANH ALY
4 ANH16 ANH ALY
4 ANH96 ANH ALY
4 ANH99 ANH ALY
5 ALF16 ALF ALY
5 ALF20 ALF ALY
So level 2 is a child of Aly, 3 is a grandchild of Aly, etc. The second column is the Mother of the given Case (whale)
Along with the level I would like to add the complete Lineage. The lineage for AME04 would be (AME04 - AME - ALY). When I try this I get a Type incompatibility.
CaseName as Lineage in the base will not UNION with "Lineage + a.CASENAME" in the recursive part
In the second part I even tried just a very simple concatenation of
Lineage + ' literal '
and get the same error.
I do not think this question is specific to the recursive CTE, but a general union question. Do I have to cast a concatenated string in this case? I tried to cast the string, but could not get the syntax correct.
Recommendation for calling Share Procedures with Parameter
I am converting my first DB to SQL Server and teaching myself to use SQL as a backend. To teach myself as much SQL Server I am doing everything on the server side even though the database is relatively small. It is however pretty complex code since it is a self refencing hierarchical database...
www.access-programmers.co.uk
Code:
WITH cte_Ancestors AS (
SELECT
1 as Level,
CASENAME,
MOT,
caseName as TOP_MOT
--,CaseName as Lineage
FROM
vw_Population
WHERE casename = 'aly'
UNION ALL
SELECT
Level + 1,
A.CaseName,
A.MOT,
TOP_MOT
--,Lineage + a.CASENAME
FROM
vw_Population as A
inner join cte_ancestors as C
on
c.casename = a.mot
)
1 ALY NULL ALY
2 AME ALY ALY
2 250 ALY ALY
2 AY9 ALY ALY
3 AME04 AME ALY
3 AME08 AME ALY
3 AME12 AME ALY
3 AME16 AME ALY
3 ANH AME ALY
3 405 AME ALY
3 ANN AME ALY
3 579 AME ALY
3 AVA AME ALY
4 AVA13 AVA ALY
4 AVA19 AVA ALY
4 ALF ANN ALY
4 ANN08 ANN ALY
4 ANN12 ANN ALY
4 ANN17 ANN ALY
4 ANH02 ANH ALY
4 ANH07 ANH ALY
4 AKI ANH ALY
4 ANH16 ANH ALY
4 ANH96 ANH ALY
4 ANH99 ANH ALY
5 ALF16 ALF ALY
5 ALF20 ALF ALY
So level 2 is a child of Aly, 3 is a grandchild of Aly, etc. The second column is the Mother of the given Case (whale)
Along with the level I would like to add the complete Lineage. The lineage for AME04 would be (AME04 - AME - ALY). When I try this I get a Type incompatibility.
Which I assume means thatTypes don't match between the anchor and the recursive part in column "Lineage" of recursive query "cte_Ancestors".
CaseName as Lineage in the base will not UNION with "Lineage + a.CASENAME" in the recursive part
In the second part I even tried just a very simple concatenation of
Lineage + ' literal '
and get the same error.
I do not think this question is specific to the recursive CTE, but a general union question. Do I have to cast a concatenated string in this case? I tried to cast the string, but could not get the syntax correct.