简体   繁体   中英

Sql Case Statement Error Cannot select More Than one Column

I need to get the Supplier ID from the SQL Select statement inside Case statement. Once I put the A.SUPPLIER_ID to Select Statement I get an Error. How to do this?

Select 
       CASE 
         WHEN  TYPE = 1 THEN 
           (
              SELECT A.name 
              from BIZZXE_V2_SCH.SUPPLIERS A
              where A.SUPPLIER_ID = 30
           ) 
        ELSE
          (
            SELECT A.name 
            from BIZZXE_V2_SCH.STOCK_SUPPLIER A
            where A.SUPPLIER_ID = 31
          )
        END name
from DUAL;

You can't put complete queries into a case statement. But this should work

SELECT name
from BIZZXE_V2_SCH.SUPPLIERS
where SUPPLIER_ID = 30 and TYPE = 1
union all
SELECT name
from BIZZXE_V2_SCH.STOCK_SUPPLIER
where SUPPLIER_ID = 31 and TYPE <> 1

You should be able to handle this in your WHERE statement, like this.

SELECT A.name 
FROM BIZZXE_V2_SCH.STOCK_SUPPLIER A
WHERE (A.SUPPLIER_ID = 30 AND TYPE = 1) OR
A.SUPPLIER_ID = 31

Using IF/ELSE statement

DECLARE @type NUMBER;
SELECT TYPE INTO @type FROM DUAL; -- Make sure it always returns one row

IF @type = 1 THEN
    SELECT A.name 
    FROM BIZZXE_V2_SCH.SUPPLIERS A
    WHERE A.SUPPLIER_ID = 30;
ELSE
    SELECT A.name 
    FROM BIZZXE_V2_SCH.STOCK_SUPPLIER A
    WHERE A.SUPPLIER_ID = 31
END IF;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM