SQL CASE WHEN语句

时间:2017-08-03 22:00:22

标签: sql database

我试图让这句话发挥作用,但它说我在" INV.CaseQty = 1"

我想了解逻辑:

when x = 1 then y = 1 else y

CASE WHEN INV.CaseUp = 1 then  INV.CaseQty = 1 ELSE INV.CaseQty end

非常感谢大家的帮助

以下是来自存储过程的SELECT查询

select C.CustomerNum, ISNULL(ST.ShiptoNum,'') Shipto, I.InvoiceNum,
       DatePart(yyyy,I.InvoiceDate) DeliveryYear,
       DatePart(qq,I.InvoiceDate) DeliveryQuarter,
       DatePart(MM,I.InvoiceDate) DeliveryMonth,
       DatePart(WW,I.InvoiceDate) DeliveryWeek,
       CAST(I.InvoiceDate as Date) DeliveryDate, INV.InventoryNum,
       IP.InventoryPackageNum, ISNULL(CaseQty, 0) QTYinEach,
       ID.Description,ISNULL(IT.Description,'No Type') SubCategory,
       CASE WHEN INV.CaseUp = 1 then  INV.CaseQty = 1
            ELSE INV.CaseQty
       end

原始表

Description                             CaseQty     caseup
Royal 5" Red Stripe Plastic Sip Stirrer 1000          1
Ecoproducts Plant Starch Knife          1000          0
Ecoproducts Plant Starch Fork           1000          1
Ecoproducts Plant Starch Spoon          1000          0 

我希望我的商店程序中的select查询能够更改CaseUp = 1的那些,CaseQty也应该更改为1。

3 个答案:

答案 0 :(得分:2)

你不能设置一个值"当您尝试使用INV.CaseQty = 1时,您正在从表中选择值

SELECT CASE WHEN INV.CaseUp = 1 THEN 1 ELSE INV.CaseQty END as CaseQty

答案 1 :(得分:0)

您描述的逻辑是不连贯的:在THEN子句中,您正在执行操作,但在ELSE子句中,您将返回一个值,并且这些操作不兼容。

分配过程将通过IF ... ELSE结构完成;根据条件返回不同的值将通过CASE结构完成。

答案 2 :(得分:0)

看起来你需要where where子句 也许像:

where 
((INV.CaseUp = 1 and INV.CaseQty = 1) or INV.CaseUp <> 1)