开发者

how to this query in right syntax or right way?

开发者 https://www.devze.com 2022-12-30 05:54 出处:网络
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 ?

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 ?

SEL开发者_开发问答ECT SUM(Quantity) 
  FROM   Invoices
 WHERE  Invoices.InvoiceDocStatusID = 
        CASE @InventoryType
                      WHEN 1 
                      THEN ((2)OR(1))
                      ELSE 2
                    END


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


  • 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)
0

精彩评论

暂无评论...
验证码 换一张
取 消