Sql server 2008 我正在sql server 2008 r2中执行此查询,结果是;子查询返回了多个值。这是不允许的;请帮帮我

Sql server 2008 我正在sql server 2008 r2中执行此查询,结果是;子查询返回了多个值。这是不允许的;请帮帮我,sql-server-2008,Sql Server 2008,我想你只是想要这样的东西: select purchase_id , (case when (select source_type from dbo.tbl_Purchase where purchase_id = tbl_Purchase.purchase_id)=1 then (select vessel_name from dbo.tbl_Vessel where vessel_type=1) when (select source_type from dbo.tbl_P

我想你只是想要这样的东西:

 select purchase_id ,

 (case when (select source_type from dbo.tbl_Purchase where purchase_id  = tbl_Purchase.purchase_id)=1 then 

 (select vessel_name from dbo.tbl_Vessel where vessel_type=1)
 when  (select source_type from dbo.tbl_Purchase where purchase_id  = tbl_Purchase.purchase_id)=2 then 
 (select vessel_name from dbo.tbl_Vessel where vessel_type=2)
 else
 (select jetty_name from dbo.tbl_Jetty )

 end ) as vessl_name
 from tbl_Purchase
否则,如果要继续执行
案例
/子查询路径,则需要修复以下语句:

 select purchase_id ,
        COALESCE(v.vessel_name,j.jetty_name) as vessl_name
 from dbo.tbl_Purchase p
         left join
      dbo.tbl_Vessel v
         on
            p.source_type in (1,2) and
            v.vessel_type = p.source_type
         left join
      dbo.tbl_Jetty j
         on
            p.source_type not in (1,2)
其中,该比较的两侧实际上都引用了相同的表和相同的列,因此它将始终返回
tbl\u Purchase
中的所有行(除了那些
NULL
Purchase\u id
的行)

有关如何在表上使用别名(
p
v
j
)的信息,请参见上面的查询,以便为每个列引用指定正确的源表。也就是说,我希望有如下情况:

(select source_type from dbo.tbl_Purchase where purchase_id  = tbl_Purchase.purchase_id)
假设外部
FROM
子句中的原始
tbl\u Purchase
已被赋予别名
p1


进一步阅读:


您必须从主查询中删除最后一个子查询,并使用join而不是 子查询。

当您执行此
(某个子查询)=1
时,子查询必须只返回一个值,否则无法进行比较。您的一个子查询返回多个。如何处理取决于问题的根本原因。也许你有一个不应该复制的地方吗?
(select source_type from dbo.tbl_Purchase p2 where p2.purchase_id  = p1.purchase_id)