Question

I have a table it's called solr. using this below Scripti'm updating this into solr table.but here my doubt is instead of this i need to use If...Else condition.

I'm not getting any idea about this one. Kindly help me with on this.

UPDATE solr
SET Stock=
  CASE
    WHEN SUD.D_price='ABC'
    OR SUD.D_price  ='CDE'
    AND WIS.StockID >0
    THEN 'Y'
    WHEN SUD.Display_price='XYZ '
    OR SUD.Display_price  ='PQR'
    AND WIS.StockID       >0
    AND WI.StockName NOT LIKE '%GGR%'
    THEN 'Y'
  END
FROM InventoryItems WIS
JOIN solr SUD
ON WIS.ProductID=SUD.ProductId
JOIN WS_Inventory WI
ON WI.StockID=WIS.StockID
Was it helpful?

Solution 2

You don't explain exactly what you want, but I surmise that you need an else clause. If you want to set the value to 'N', then use:

UPDATE solr
    SET Stock = (case when SUD.D_price = 'ABC' OR SUD.D_price = 'CDE' AND WIS.StockID > 0
                      THEN 'Y' 
                      WHEN SUD.Display_price='XYZ ' OR SUD.Display_price='PQR' AND WIS.StockID>0 AND WI.StockName NOT LIKE '%GGR%' 
                      THEN  'Y' 
                      ELSE 'N'
                 end)
   from solr SUD join
        InventoryItems WIS
        on WIS.ProductID = SUD.ProductId JOIN
        WS_Inventory WI
        ON WI.StockID = WIS.StockID;

Perhaps you want:

                      ELSE stock

This will not change the value. You can also put conditions in the where clause to only update to Y, if that is your intention. I don't see a need for if.

OTHER TIPS

I don't really understand your question, but I do know this... whenever you mix AND with OR in a condition, you should use parenthesis to control the order of operations.

  CASE
    WHEN (SUD.D_price='ABC' OR SUD.D_price  ='CDE') 
         AND WIS.StockID >0
    THEN 'Y'
    WHEN (SUD.Display_price='XYZ' OR SUD.Display_price ='PQR') 
         AND WIS.StockID > 0
         AND WI.StockName NOT LIKE '%GGR%'
    THEN 'Y'
    ELSE -- You should probably have some value here.
  END

Dont know about the data at all but my guess is you nested ANDs and ORs are messing it up use paranthesis to force the order in which they are evaluated, as the default behaviour is NOT takes presedence over AND , AND takes presedence over OR.

NOT --> AND --> OR

this can be a bit confusing if you dont pay much attention to in what order sql server is going to evaluate each expression. Using Paranthesis makes is much simpler.

UPDATE SUD
SET SUD.Stock = CASE
                    WHEN (SUD.D_price='ABC' OR SUD.D_price  ='CDE') 
                             AND WIS.StockID > 0
                       THEN 'Y'
                    WHEN (SUD.Display_price='XYZ ' OR SUD.Display_price  ='PQR') 
                        AND WIS.StockID > 0  AND WI.StockName NOT LIKE '%GGR%'
                       THEN 'Y'
                   --ELSE 'N'  --<-- Default value
                  END
FROM InventoryItems WIS INNER JOIN solr SUD
ON WIS.ProductID = SUD.ProductId
INNER JOIN WS_Inventory WI
ON WI.StockID = WIS.StockID

You could add ELSE..

UPDATE  solr
SET     Stock = CASE WHEN SUD.D_price = 'ABC'
                          OR SUD.D_price = 'CDE'
                          AND WIS.StockID > 0 THEN 'Y'
                     WHEN SUD.Display_price = 'XYZ '
                          OR SUD.Display_price = 'PQR'
                          AND WIS.StockID > 0
                          AND WI.StockName NOT LIKE '%GGR%' THEN 'Y'
                     ELSE 'N'
                END
FROM    InventoryItems WIS
        JOIN solr SUD ON WIS.ProductID = SUD.ProductId
        JOIN WS_Inventory WI ON WI.StockID = WIS.StockID

I've just assumed N in this case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top