Dang these cursed iterative CTEs!
I did like you said and started simple, building step by step. I think I have the outline for a BAQ that should work, however I keep getting Syntax OK, and Bad SQL Statement.
with [JobsCTE] as
(select
[JobAsmbl].[JobNum] as [JobAsmbl_JobNum],
[JobAsmbl].[AssemblySeq] as [JobAsmbl_AssemblySeq],
[JobAsmbl].[PartNum] as [JobAsmbl_PartNum],
[JobAsmbl].[Parent] as [JobAsmbl_Parent],
(0) as [Calculated_Hierarchy],
(cast(substring('...',1,(Hierarchy +1) ) + JobAsmbl.Parent as nvarchar(25))) as [Calculated_Ind1]
from Erp.JobAsmbl as JobAsmbl
where (JobAsmbl.JobNum = '32030/1' and JobAsmbl.AssemblySeq = 0)
union all
select
[JobAsmbl1].[JobNum] as [JobAsmbl1_JobNum],
[JobAsmbl1].[AssemblySeq] as [JobAsmbl1_AssemblySeq],
[JobAsmbl1].[PartNum] as [JobAsmbl1_PartNum],
[JobsCTE1].[JobAsmbl_Parent] as [JobAsmbl_Parent],
(Calculated_Hierarchy + 1) as [Calculated_Hierarchy2],
(cast(substring('......',1,(Hierarchy2 +1) ) + JobsCTE1.JobAsmbl_Parent as nvarchar(25))) as [Calculated_Ind2]
from Erp.JobAsmbl as JobAsmbl1
inner join JobsCTE as JobsCTE1 on
JobAsmbl1.JobNum = JobsCTE1.JobAsmbl_JobNum
and JobAsmbl1.Parent = JobsCTE1.JobAsmbl_AssemblySeq)
select
[JobsCTE].[JobAsmbl_JobNum] as [JobAsmbl_JobNum],
[JobsCTE].[JobAsmbl_AssemblySeq] as [JobAsmbl_AssemblySeq],
[JobsCTE].[JobAsmbl_PartNum] as [JobAsmbl_PartNum],
[JobsCTE].[JobAsmbl_Parent] as [JobAsmbl_Parent],
[JobsCTE].[Calculated_Hierarchy] as [Calculated_Hierarchy],
[JobsCTE].[Calculated_Ind1] as [Calculated_Ind1],
[JobOper].[OprSeq] as [JobOper_OprSeq]
from JobsCTE as JobsCTE
inner join Erp.JobOper as JobOper on
JobsCTE.JobAsmbl_JobNum = JobOper.JobNum
and JobsCTE.JobAsmbl_AssemblySeq = JobOper.AssemblySeq
This is how I understand it.
Subquery 1 - JobsCTE. This is a CTE subquery that returns all the parts in JobAsm where JobNum = user input. This contains the Parent field which shows the level of the parent asm.
Subquery 2 - JobsUnion. This is a UnionAll subquery that links JobAsm with the CTE table. It links on JobAsmbl.JobNum=JobsCTE.JobNum and JobAsmbl.AsmSeq = JobsCTE.Parent.
TopLevel Query 3 - The Top Level. This just returns everything from JobsCTE, and links up the operation table to try to pull in the ops for the Asms that get returned.
Am I missing something obvious? I have tried various iterations using JobAsm.Parent, JobASM.AsmSeq, JobAsm.PartNum. But all iterations gave me the same results of Syntax ok, Bad SQL.
'I also tried this:
inner join JobsCTE as JobsCTE1 on
JobAsmbl1.JobNum = JobsCTE1.JobAsmbl_JobNum
and JobAsmbl1.AssemblySeq = JobsCTE1.Parent)
'and tried this:
inner join JobsCTE as JobsCTE1 on
JobAsmbl1.JobNum = JobsCTE1.JobAsmbl_JobNum
and JobAsmbl1.PartNum = JobsCTE1.JobAsmbl_PartNum)
'(I think I got some results using this approach but it wasn't right.)