views:

39

answers:

2

i write this query using t-sql and give me an error (Incorrect syntax near the keyword 'OR') How to write it in correct way ?

SELECT SUM(Quantity) 
  FROM   Invoices
 WHERE  Invoices.InvoiceDocStatusID = 
        CASE @InventoryType
                      WHEN 1 
                      THEN ((2)OR(1))
                      ELSE 2
                    END
+1  A: 
  • You're always checking for Invoices.InvoiceDocStatusID = 2
  • The only switch is on @InventoryType
  • No ELSE clause is needed: it goes to NULL

So...

SELECT SUM(Quantity) 
FROM Invoices
WHERE Invoices.InvoiceDocStatusID IN (2, CASE @InventoryType WHEN 1 THEN 1 END)
gbn
+2  A: 

Not really that certain of the desired semantics. Do you always want rows matching InvoiceDocStatusID=2 regardless of the value of @InventoryType and additionally want InvoiceDocStatusID=1 where @InventoryType = 1?

SELECT SUM(Quantity) 
  FROM   Invoices
 WHERE  (Invoices.InvoiceDocStatusID = 1 AND @InventoryType = 1)
OR Invoices.InvoiceDocStatusID = 2
Martin Smith