如何在案件陈述中适用条件

时间:2016-01-15 06:15:57

标签: sql sql-server sql-server-2008

SELECT     Sales_Contract.ContNo, Sales_Contract.Date, PartyMst.PName as PartyName, Customer.CustName as Customer, 
           ItemMst.ItName as ItemName, Sales_Contract.Grade, 
           Sales_Contract.Rate, Sales_Contract.Weight,
           case when @Pending=1 then (Sales_Contract.BalWeight > 0)  else Sales_Contract.BalWeight  end 
FROM         Sales_Contract LEFT OUTER JOIN
                      ItemMst ON Sales_Contract.ItCd = ItemMst.ItCd LEFT OUTER JOIN
                      Customer ON Sales_Contract.Customer = Customer.CustCd LEFT OUTER JOIN
                      PartyMst ON Sales_Contract.SPartyCd = PartyMst.PartyCd

在我的声明case when @pending=1中,我希望BalWeight>0显示表Sales_Contract

中的所有值

1 个答案:

答案 0 :(得分:1)

Where子句中添加过滤器,而不是select。试试这个

SELECT sales_contract.contno, 
       sales_contract.date, 
       partymst.pname    AS PartyName, 
       customer.custname AS Customer, 
       itemmst.itname    AS ItemName, 
       sales_contract.grade, 
       sales_contract.rate, 
       sales_contract.weight
FROM   sales_contract 
       LEFT OUTER JOIN itemmst 
                    ON sales_contract.itcd = itemmst.itcd 
       LEFT OUTER JOIN customer 
                    ON sales_contract.customer = customer.custcd 
       LEFT OUTER JOIN partymst 
                    ON sales_contract.spartycd = partymst.partycd 
WHERE  ( @pending = 1 
         AND sales_contract.balweight > 0 ) 
        OR @pending <> 1