Multi-part identifier could not be bound
I know that there are several questions around this exception on SO, but nothing seen that helps me.
I have following query giving me a "Multi-part identifier 'claim.fiData' could not be bound"
-Exception:
SELECT claim.idData FROM tabData as claim
INNER JOIN dbo._previousClaimsByFiData(claim.fiData) AS prevClaim
ON prevClaim.idData=claim.fiData
GROUP BY claim.idData
HAVING(prevClaim.fiMaxActionCode IN (8, 23, 24) and
prevClaim.Repair_Completion_Date >= DATEADD(day,-90,prevClaim.Repair_Completion_Date))
ORDER BY claim.idData
previousClaimsByFiData
is a Table-Valued-Function that returns all previous records. You can find it here if you're interested.
Now i want to find all claims that have previous claims in the last 90 days with a maxActionCode IN(8, 23, 24).
What i've also tried was following:
SELECT count(*) FROM tabData as claim
where exists(
select 1 from dbo._previousClaimsByFiData(claim.fiData)as prevClaim where
prevClaim.fiMaxActionCode IN(8, 23, 24)and
prevClaim.Repair_Completion_Date >= DATEADD(day,-90,claim.Repair_Completion_Date)
)
But that gives me a "The maximum recursion 100 has been exhausted before statement completion"
-Exception.
Why do i get those exceptions and how to avoid them?
Thank you
EDIT: asked another question which is reduced on the main problem. I can delete this when i get an answer there.
UPDATE: Marc answered a simplified question according to this here. So the way to go is Cross Apply. But now i have the next prob开发者_StackOverflow中文版lem what i've already mentioned above. I get a "The maximum recursion 100 has been exhausted before statement completion" Error after a few seconds. I dont know where to add the OPTION (MAXRECURSION 0) because i get a "Incorrect Syntax" if i try to add it in the Inline-TVF.
My current query is:
SELECT claim.idData FROM tabData claim
CROSS APPLY dbo._previousClaimsByFiData(claim.fiData)AS tvfData
GROUP BY claim.idData,claim.Repair_Completion_Date,tvfData.Repair_Completion_Date,tvfData.fiMaxActionCode
HAVING(tvfData.fiMaxActionCode IN (8, 23, 24) and
tvfData.Repair_Completion_Date >= DATEADD(day,-90,claim.Repair_Completion_Date))
ORDER BY claim.idData
UPDATE: the solution was to add the OPTION (MAXRECURSION 0) to the end of the SELECT Statement.
The solution was to add the OPTION (MAXRECURSION 0) to the end of the SELECT Statement. The working query is now:
SELECT claim.idData,claim.Repair_Completion_Date,prevClaim.Repair_Completion_Date,prevClaim.fiMaxActionCode FROM tabData claim
CROSS APPLY dbo._previousClaimsByFiData(claim.fiData)AS prevClaim
GROUP BY claim.idData,claim.Repair_Completion_Date,prevClaim.Repair_Completion_Date,prevClaim.fiMaxActionCode
HAVING(prevClaim.fiMaxActionCode IN (8, 23, 24) and
prevClaim.Repair_Completion_Date >= DATEADD(day,-90,claim.Repair_Completion_Date))
ORDER BY claim.idData
OPTION (MAXRECURSION 0)
It's not the fastest query(takes more than a minute to get 23000 records in a >11Mio rec table) but it works.
UPDATE: The following query is much faster(<4 seconds) and uses EXISTS
without CROSS APPLY
:
SELECT idData
FROM tabData AS claim
WHERE fiProductType=1 and fiClaimStatus IN(1,5,7,8,9) AND EXISTS
(SELECT idData
FROM dbo._previousClaimsByFiData(claim.fiData) AS prevClaim
WHERE (fiProductType = 1) AND (fimaxActionCode IN (8, 23, 24))
AND (Repair_Completion_Date >= DATEADD(dd, - 90, DATEADD(dd, DATEDIFF(dd,0, claim.Repair_Completion_Date), 0)))
AND (Repair_Completion_Date <= claim.Repair_Completion_Date))
order by claim.idData
OPTION (MAXRECURSION 0)
精彩评论