sql - 无法绑定(bind)多部分标识符

标签 sql sql-server

为什么会出现此错误:

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                                     

最佳答案

你不能这样做,这样。 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)) + '%';

...

关于sql - 无法绑定(bind)多部分标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14004554/

相关文章:

sql - Oracle : Query Runs Successfully, 但列名不可用

mysql - 重写 MySQL 的 SQL Server 查询

mysql - SQL 根据表中的值插入表中

javascript - 如何在选择查询中分配字符串变量

c# - 如何从代码中区分 MySQL 和 MS SQL 数据库?

php - 选择数据并在所选记录中查找重复值的sql查询

sql - 如何通过SQL查询在组中添加选择?

当第二个为空时 SQL 内部连接在一个字段上,当第一个为空时在第二个字段上连接

mysql - 带有归类 "utf8_general_ci"错误的 MySQL 表连接

sql - 计算第 95 个百分位值?