2012-12-22 38 views
3

爲什麼我得到這個錯誤:多部分組成的標識符無法綁定

The multi-part identifier could not be bound on line 18
The multi-part identifier "ShipTB.sh_Type" could not be bound.
Line 32 the multi-part identifier "ShipTB.sh_Type" could not be bound.

CREATE PROCEDURE DocumentReportProc 
(
    @searchOpt tinyint, 
    @keyser nvarchar (50), 
    @usertypeid tinyint output 
) 
as 
SELECT  
    ExceptionTB.excep_ref, ExceptionTB.emo_no, ShipTB.sh_Type 
FROM ExceptionTB 
INNER JOIN ShipTB ON ExceptionTB.emo_no = ShipTB.Emo_No 
where ExceptionTB.excep_ref like cast(@keyser as varchar(50)) + '%' 

IF ((ShipTB.sh_Type)=('تجارية')) 
BEGIN 

SELECT 
    ExceptionTB.excep_ref, 
    ExceptionTB.emo_no, 
    ExceptionTB.broker, 
    ExceptionTB.r_date, 
    ShipTB.S_Name, 
    ShipTB.sh_Type, 
    ArrivalNotiDate.Arri_noti_date, 
    ArrivalNotiDate.port, 
    CargoCertificate.[1], 
    CargoCertificate.[2], 
    CargoCertificate.[3], 
    CargoCertificate.[4], 
    CargoCertificate.[5], 
    CargoCertificate.[6], 
    CargoCertificate.[7], 
    CargoCertificate.[8] 
    FROM   ExceptionTB 
    INNER JOIN ShipTB ON ExceptionTB.emo_no = ShipTB.Emo_No 
    INNER JOIN ArrivalNotiDate ON ExceptionTB.excep_ref = ArrivalNotiDate.exp_ref 
    INNER JOIN CargoCertificate ON ExceptionTB.excep_ref = CargoCertificate.excep_ref  
    where ExceptionTB.excep_ref like cast(@keyser as varchar(50)) + '%' 

END  
IF (ShipTB.sh_Type='سياحية') 
BEGIN 
SELECT  
    ExceptionTB.excep_ref, 
    ExceptionTB.emo_no, 
    ExceptionTB.broker, 
    ExceptionTB.r_date, 
    ShipTB.S_Name, 
    ShipTB.sh_Type, 
    ArrivalNotiDate.Arri_noti_date, 
    ArrivalNotiDate.port, 
    PassengerCertificate.[1], 
    PassengerCertificate.[2], 
    PassengerCertificate.[3], 
    PassengerCertificate.[4], 
    PassengerCertificate.[5], 
    PassengerCertificate.[6] 
    FROM   dbo.ExceptionTB 
    INNER JOIN ShipTB ON ExceptionTB.emo_no = ShipTB.Emo_No 
    INNER JOIN ArrivalNotiDate ON ExceptionTB.excep_ref = ArrivalNotiDate.exp_ref 
    INNER JOIN PassengerCertificate ON ExceptionTB.excep_ref = PassengerCertificate.excep_ref 
    where ExceptionTB.excep_ref like cast(@keyser as varchar(50)) + '%'      
END          

回答

0

你不能做到這一點,這條路。 IF語句中的ShipTB.sh_Type是一個結果集,它是一個包含多個值的字段。此外,它不能參考SELECT條款之外。如果該字段ShipTB.sh_Type應該只包含一個值,那麼您可以將其選擇爲標量變量,如@type,然後將其置於該條件中。

否則,如果該字段ShipTB.sh_Type包含更多的價值,那麼我認爲你正在嘗試做一個有條件的加入,在這種情況下,你可以這樣做,在一個查詢,像這樣:

... 

SELECT  
    ExceptionTB.excep_ref, 
    ExceptionTB.emo_no, 
    ExceptionTB.broker, 
    ExceptionTB.r_date, 
    ShipTB.S_Name, 
    ShipTB.sh_Type, 
    ArrivalNotiDate.Arri_noti_date, 
    ArrivalNotiDate.port, 
    PassengerCertificate.[1], 
    PassengerCertificate.[2], 
    PassengerCertificate.[3], 
    PassengerCertificate.[4], 
    PassengerCertificate.[5], 
    PassengerCertificate.[6] 
    CargoCertificate.[1], 
    CargoCertificate.[2], 
    CargoCertificate.[3], 
    CargoCertificate.[4], 
    CargoCertificate.[5], 
    CargoCertificate.[6], 
    CargoCertificate.[7], 
    CargoCertificate.[8] 
FROM  ExceptionTB 
INNER JOIN ShipTB     
     ON ExceptionTB.emo_no = ShipTB.Emo_No 
INNER JOIN ArrivalNotiDate  
     ON ExceptionTB.excep_ref = ArrivalNotiDate.exp_ref 
INNER JOIN CargoCertificate  
     ON ExceptionTB.excep_ref = CargoCertificate.excep_ref 
     AND ShipTB.sh_Type  = 'تجارية' 
INNER JOIN PassengerCertificate 
     ON ExceptionTB.excep_ref = PassengerCertificate.excep_ref 
     AND ShipTB.sh_Type  = 'سياحية' 
WHERE  ExceptionTB.excep_ref LIKE CAST(@keyser as varchar(50)) + '%'; 

... 
相關問題