1

我想在 case 语句中返回多行。是否可以?还是有其他方法可以做到这一点?

select 
   case 
      when 3 = 1 
          then (select orderid from order_master where noOfInstallment = installmentPaid) 
      else 
          (select orderid from order_master where noOfInstallment <> installmentPaid) 
   END

两个子查询都返回多行。现在上面的查询显示以下错误。

子查询返回超过 1 个值。当子查询跟随 =、!=、<、<=、>、>= 或子查询用作表达式时,这是不允许的。

4

3 回答 3

2

SQL Server 中的 CASE不是流控制语句(它与switchC# 中的语句不同)——它只是用于返回几个可能值之一。

您需要IF在 T-SQL 中使用语句

IF 3 = 1
   select orderid from order_master where noOfInstallment = installmentPaid
ELSE
   select orderid from order_master where noOfInstallment <> installmentPaid

或类似的东西。

于 2011-12-29T07:51:48.037 回答
0

您没有以正确的方式使用 case 语句,请查看文档

你能用这样的查询来管理吗:

select orderid,  
   case 
      when (noOfInstallment = installmentPaid) then
        'equal'   
      else 
        'not equal'
   END
from order_master
于 2011-12-29T08:05:14.527 回答
0

我得到了解决方案。

 SELECT o.* FROM Order_Master as o Where 
   (
      ((@paymentStatus = 0) AND (o.noOfInstallment <> o.installmentPaid))
      OR 
      ((@paymentStatus=1) AND (o.noOfInstallment = o.installmentPaid)) 
   )
于 2011-12-31T10:23:51.443 回答