2012-11-02 2 views
0

3 개의 다른 테이블에서 데이터를 가져 오는 SQL 쿼리를 만들려고합니다. 내 생각은 CTE를 사용하여 2 개의 테이블에서 정보를 수집 한 다음 오른쪽 조인을 수행하여 나머지 쿼리에 데이터를 추가하는 것이 었습니다.CTE 다른 ID 필드에 연결

Msg 4104, Level 16, State 1, Line 1 
The multi-part identifier "V_PhysicianList.Doctor_Number" could not be bound. 
Msg 4104, Level 16, State 1, Line 1 
The multi-part identifier "PhysicianFile.Doctor Number" could not be bound. 

목표 여기 : 여기

with cte1 as 
(
SELECT   
[Physician ID] as InternalIdentifier, NPI, [Last Name] as LastName, [First Name]  
as  
FirstName, 
Physician_T1HYCPP.Specialty, DOB, Degree, Department, [State License Number], [UPIN Number],  
[Physician Status] 
FROM Physician_T1HYCPP left outer JOIN PhysicianFile 
on Physician_T1HYCPP.[Physician ID] = PhysicianFile.[TSI MD Number] 
where NPI <> '' 
), 
cteView 
AS 
(
Select [Doctor_Number], Address, City, State, Zip, Phone, Fax 
from V_PhysicianList 
) 
Select 
InternalIdentifier, NPI, LastName, FirstName, 
Specialty, DOB, Degree, Department, [State License Number], [UPIN Number], 
[Physician Status],  
[Doctor_Number], 
Address,City, State, Zip, Phone, Fax 
from cte1 
right outer join cteView on 
V_PhysicianList.[Doctor_Number] = PhysicianFile.[Doctor Number] 

가 특정 오류입니다 : 나는 여기에 지금까지 작성한 무엇 각 필드에서 서로 다른 데이터를 가져올 필요하지만, 악명 높은 오류 multi-part identifier could not be bound으로 실행하고 첫 번째 CTE의 2 개 표에서 모든 입력란을 가져 와서 두 번째 CTE의 입력란에 '병합'하여 주소 등이 최종 결과 집합에서 가치를 갖게하는 것입니다. cte1과 cteView의 필드가 올바르게 병합되었는지 어떻게 확인할 수 있습니까?

+0

번호를 포함하여 정확한 오류 메시지를 게시 할 수 있습니까? –

+0

내기를 걸었습니다. 그냥 추가했습니다. – SidC

+0

작은 들여 쓰기는 먼 길을 갈 것입니다 ... – RBarryYoung

답변

2

마지막 SELECT에서 CTE를 선택하지만 JOIN 절의 기본 테이블을 참조하십시오. 올바른 접두사를 사용하십시오.

Select 
InternalIdentifier, NPI, LastName, FirstName, 
Specialty, DOB, Degree, Department, [State License Number], [UPIN Number], 
[Physician Status],  
[Doctor_Number], 
Address,City, State, Zip, Phone, Fax 
from cte1 
right outer join cteView on 
cteView.[Doctor_Number] = cte1.[Doctor Number] 

그러나 해당 열을 cte1에 포함시켜야합니다. 최종 선택 중에 SQL Server는 거기에 언급 된 테이블, 뷰 또는 CTE에 대한 액세스 만 가지므로 기본 테이블에 대한 참조를 확인할 수 없습니다.