我有一个我想运行的SQL查询,如果我将参数标记为true,则想要在where子句中添加一些内容.我不认为我需要两次相同的sql语句,但找不到这样做的方法.这就是我要的.
DECLARE @getShipped VARCHAR = 'false'; SELECT DISTINCT Serial_No INTO #Serials FROM Part_v_Container_Change2 AS CC WHERE Change_Date <= @dateEnding *** AND IF @getShipped = 'true' THEN CC.Container_Status = 'Shipped' ***
尝试过if语句和case语句但似乎无法使其工作?如果我没有,我只是不想重复sql.
试试吧
DECLARE @getShipped VARCHAR(20) = 'false'; SELECT DISTINCT Serial_No INTO #Serials FROM Part_v_Container_Change2 AS CC WHERE Change_Date <= @dateEnding AND ((@getShipped = 'true' AND CC.Container_Status = 'Shipped') OR @getShipped = 'false')