简体   繁体   中英

SQL - WHERE with CASE statement

   SELECT TOP 1
        CostValue 
    FROM 
        [~client_table~].[dbo].[CostRules] AS CostRule 
    WHERE 
        (CASE
            WHEN DATALENGTH(CostRule.ModelName) = 0 
            THEN 
                CostRule.Type = 1
                AND CostRule.Manufacturer = Printer.ManufacturerId
                AND CostRule.ColorType = 1
            ELSE
                CostRule.Type = 2
                AND CostRule.ModelName = Printer.ModelName
                AND CostRule.ColorType = 1
            END
        )
    ) AS MonoCost

I want to define my where statement depending on the datalength of CostRule.ModelName . But i got an error: Incorrect syntax near '=' . in CostRule.Type = 1 and i got a error in the ELSE statement.

Must be like this:

...
WHERE
  (DATALENGTH(CostRule.ModelName) = 0
    AND CostRule.Type = 1
    AND CostRule.Manufacturer = Printer.ManufacturerId
    AND CostRule.ColorType = 1)
  OR
  (DATALENGTH(CostRule.ModelName) != 0
    AND CostRule.Type = 2
    AND CostRule.ModelName = Printer.ModelName
    AND CostRule.ColorType = 1)

The CASE -style from your query cannot work.

you can change your statement like this:

SELECT TOP 1
    CostValue 
FROM 
    [~client_table~].[dbo].[CostRules] AS CostRule 
WHERE CostRule.ColorType=1
AND CostRule.Type=CASE WHEN  DATALENGTH(CostRule.ModelName) = 0  THEN 1 ELSE 2 END 
AND CostRule.Manufacturer=CASE WHEN  DATALENGTH(CostRule.ModelName) = 0 THEN Printer.ManufacturerId ELSE  Printer.ModelName END 

You can't use a CASE statement to define where conditions like that. It will be easier to just use some boolean logic

SELECT *
FROM your_table
WHERE (DATALENGTH(CostRule.ModelName) = 0 
       AND CostRule.Type = 1
       AND CostRule.Manufacturer = Printer.ManufacturerId
       AND CostRule.ColorType = 1)
OR (DATALENGTH(CostRule.ModelName) != 0
       AND CostRule.Type = 2
       AND CostRule.ModelName = Printer.ModelName
       AND CostRule.ColorType = 1)

There are some other things that could be removed (like CostRule.ColorType = 1 since it is the same in both branches) but I've left them in here to illustrate how to transform your CASE statement into a boolean logic set.

It looks like you would just need to change the WHERE statement:

It looks like you will just need to change your WHERE statement to use OR and remove the CASE Statement.

(SELECT TOP 1
        CostValue 
    FROM 
        [~client_table~].[dbo].[CostRules] AS CostRule 
    WHERE 
                DATALENGTH(CostRule.ModelName) = 0 
                CostRule.Type = 1
                AND CostRule.Manufacturer = Printer.ManufacturerId
                AND CostRule.ColorType = 1
      OR
                DATALENGTH(CostRule.ModelName) <> 0  
                AND CostRule.Type = 2
                AND CostRule.ModelName = Printer.ModelName
                AND CostRule.ColorType = 1

     ) AS MonoCost

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