Case语句中的多个when语句

时间:2014-11-17 11:24:18

标签: sql-server

我的SP如下:

ALTER PROC ME_SP_GETORDERASSIGNMENT_DETAILS                          
@COMPANY_CODE  VARCHAR(5),                                                                  
@TRANSID VARCHAR(10),                                                                  
@FYEAR VARCHAR(5),                                                                  
@SMODE VARCHAR(2),                                                                  
@USERID VARCHAR(5),                                                                  
@PLANT_CODE NUMERIC(5,0),                                                                  
@TRANSNO VARCHAR(10),                                                                  
@STATUS_CODE VARCHAR(10),          
@FROM_DATE VARCHAR(15)=NULL,          
@TO_DATE VARCHAR(15)=NULL                                                                  
AS 
--New SP created By Ashwini on 11-Nov-2014 for getting order assignment details 
BEGIN

DECLARE @SAGENT_CODE VARCHAR(10)                                                  
SELECT @SAGENT_CODE = A.SAGENT_CODE FROM SD_SAGENT_C A                              
INNER JOIN GL_USER_C B ON A.PERSONNEL_ID = B.PERSONNEL_ID AND B.USER_ID = @USERID

SELECT CAST(@PLANT_CODE as Varchar) AS PLANT_CODE, @TRANSNO AS  TRANSACTIONNO,@TRANSID AS TRANSID,@USERID AS USERID,
CAST(0 AS BIT) AS PROCESS, 
CAST(0 AS BIT) AS CANCEL,                                  
isnull(F.CUSTOMER_NO,'') AS ASSIGN_COURIER,                                                                
isnull(F.NAME1,'') AS ASSIGN_COURIER_NAME,                                                                 
isnull(F.CUSTOMER_CLASS,'') AS DELIVERY_MODE,                                                                 
A.PAYMENT_TYPE,                                                                 
A.PAYMENT_MODE,                                                                
A.ORDER_STATUS AS STATUS_CODE,                                                   
E.DESCRIPTION AS STATUS_DESC,                                                                
A.ORDER_NO, A.ORDER_DATE,                                                                 
A.CUST_CODE ,                                                                
(ISNULL(C.CUST_FNAME,'') + '' + ISNULL(C.CUST_MNAME,'') +''+ISNULL(C.CUST_LNAME,'')) CUSTOMER_NAME,                         
''CUSTOMER_ADDRESS, B.ITEM_NO AS LINE_ITEM_NO,                          
B.MAT_CODE,D.DESCRIPTION AS MAT_DESC ,ISNULL(D.DRAWING_NUMBER,'') AS VENDOR_NO, B.QTY, C.CUST_PSTATE AS STATE, '' AS TALUKA,C.CUST_PCITY AS DISTRICT ,C.CUST_PPINCODE AS PINCODE,                          
CAST(1 AS BIT) AS MANULAUTO, '' AS ASSIGN_CUSTOMER1 ,                         
C.CUST_PPOSTOFFICE AS POST_OFF ,C.CUST_PPONAME AS PO_NAME, B.ITEM_SALETYPE , G.DESCRIPTION as PRIORITY, H.DESCRIPTION AS PAYMENT_DESC                        

FROM                                                                 
 RT_ORDER_H A                                                                
INNER JOIN                                                                
 RT_ORDER_T B                                                                
ON                                                                
 A.ORDER_NO = B.ORDER_NO                                                                
INNER JOIN                                                                
 RT_CUSTADD_M C                                                                  
ON A.CUST_CODE = C.CUST_CODE                                                         
INNER JOIN MM_MATERIAL_BASIC_M D                                                        
ON B.MAT_CODE=D.MAT_CODE
LEFT OUTER JOIN                                                    
 RT_ORDER_STATUS E                                                    
ON A.ORDER_STATUS = E.STATUS_CODE       
LEFT JOIN SD_CUSTADD_M F ON A.DELIVERY_BY = F.CUSTOMER_NO                         
LEFT JOIN RT_ORDER_PRIORITY_C G ON A.ORDER_PRIORITY = G.PRIORITY_TYPE                        
INNER JOIN RT_PAYTYPE_C H ON A.PAYMENT_TYPE = H.PAYMENT_TYPE 
INNER JOIN MM_MATERIAL_PARENT_GRP_D I ON B.MAT_CODE=I.MAT_CODE                       

WHERE  A.ORDER_STATUS=@STATUS_CODE and year(a.ORDER_DATE)>=2014           
--END              

AND A.PAYMENT_TYPE = CASE WHEN @STATUS_CODE= '00' then 'PT0001' else A.PAYMENT_TYPE end                   

AND ( F.SAGENT_CODE = ISNULL( @SAGENT_CODE, 'XX') OR C.CUST_PSTATE_CODE IN ( SELECT STATE_CODE FROM SD_SAGENT_STATE_C WHERE SAGENT_CODE =@SAGENT_CODE))
AND I.PARENT_DESC='Vendor Offers' AND I.ACTIVE=1
and     
    CASE          
         WHEN (isnull(@FROM_DATE,'')<>'' and ISNULL(@TO_DATE,'')='') THEN Convert(date,A.order_date) >=CONVERT(datetime,@FROM_DATE,105)        
         WHEN isnull(@FROM_DATE,'')='' and ISNULL(@TO_DATE,'')<>'' THEN  Convert(date,A.order_date)<=CONVERT(datetime,@TO_DATE,105)       
         WHEN isnull(@FROM_DATE,'')<>'' and ISNULL(@TO_DATE,'')<>'' THEN  Convert(date,A.order_date)<=CONVERT(datetime,@TO_DATE,105) and CONVERT(Date,A.ORDER_DATE)>=CONVERT(datetime,@FROM_DATE ,105))          
    END  
ORDER BY A.ORDER_PRIORITY DESC,C.CUST_PSTATE,D.DESCRIPTION 
END

此SP在“&gt;”附近出现错误“语法错误。”

我必须这样做,如果@FROM_DATE和@TO_DATE两个空白然后在语句AND I.ACTIVE = 1之后没有条件应该添加。如果@FROM_DATE不是空白且@TO_DATE是空白那么一个条件应该像这样添加所有场景。 如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

涉及CASE的谓词未正确形成:

AND CASE
      WHEN ... THEN ...
      WHEN ... THEN ...
    END -- missing a condition here

要解决此问题,请按照以下方式重新编写CASE语句:

AND
CASE          
     WHEN (isnull(@FROM_DATE,'')<>'' and ISNULL(@TO_DATE,'')='') and  Convert(date,A.order_date) >=CONVERT(datetime,@FROM_DATE,105) THEN 1
     WHEN isnull(@FROM_DATE,'')='' and ISNULL(@TO_DATE,'')<>'' and Convert(date,A.order_date)<=CONVERT(datetime,@TO_DATE,105) THEN 1
     WHEN isnull(@FROM_DATE,'')<>'' and ISNULL(@TO_DATE,'')<>'' and Convert(date,A.order_date)<=CONVERT(datetime,@TO_DATE,105) and CONVERT(Date,A.ORDER_DATE)>=CONVERT(datetime,@FROM_DATE ,105)) THEN 1
     ELSE 0
END = 1