case statement returns multiple rows
-
08-04-2021 - |
Question
I want to return multiple rows in case statement. is it possible? or is there any alternate way to do it?
select
case
when 3 = 1
then (select orderid from order_master where noOfInstallment = installmentPaid)
else
(select orderid from order_master where noOfInstallment <> installmentPaid)
END
Both sub queries returns multiple rows. Now above query showing following error.
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Solution 3
I got solution.
SELECT o.* FROM Order_Master as o Where
(
((@paymentStatus = 0) AND (o.noOfInstallment <> o.installmentPaid))
OR
((@paymentStatus=1) AND (o.noOfInstallment = o.installmentPaid))
)
OTHER TIPS
CASE in SQL Server is not a flow control statement (it's different than the switch
statement in C#) - it's just used to return one of several possible values.
You need to use IF
statements in T-SQL
IF 3 = 1
select orderid from order_master where noOfInstallment = installmentPaid
ELSE
select orderid from order_master where noOfInstallment <> installmentPaid
or something like this.
You are not using the case statement in a right way, please take alook at the documentation.
Can you manage with a query like this:
select orderid,
case
when (noOfInstallment = installmentPaid) then
'equal'
else
'not equal'
END
from order_master