Where子句中的参数Case语句

时间:2016-06-27 21:39:17

标签: parameters sql-server-2012

如果是'Millwork'或'Not Millwork',需要分开订单。参数允许用户在生成报告时选择订单类型。

将参数Case语句从Select to Stack entry #6545664

中的Where子句移动

整个报表在SQL Server中成功返回数据,但在Report Server中没有。

@OrderClass是参数问题。它返回两个值:MillworkOnly和NonMillwork

--Parameter @OrderClass
SELECT DISTINCT 
CASE 
WHEN XGENERALDESCRIPTION LIKE 'MILLWORK%' THEN 'MillworkOnly'
ELSE 'NonMillwork'
END
FROM SPV3SALESDOCUMENT

以下声明返回主报告数据。它在SQL Server 2012中成功运行,但只有在Where子句末尾的Case语句被注释掉时才会在Report Server中成功运行(如图所示)。

怀疑问题可能是主语句运行时参数结果尚未可用。

参数如何在Where子句中运行?

SELECT DISTINCT
H.DOCID
, H.CUSTNAME
, H.CUSTNMBR
, H.PRSTADCD
, L.ITEMCODE
, H.ShipToName
, H.ADDRESS1
, H.ADDRESS2
, H.ADDRESS3
, H.CITY
, H.STATE
, H.ZIPCODE
, H.SLPRSNID
, H.SALSTERR
, H.MODIFDT
, H.SOPSTATUS
, H.SOPTYPE
, H.SOPNUMBE
, H.CSTPONBR
, H.SHIPMTHD
, L.LNITMSEQ
, L.ITEMNMBR
, L.ITEMDESC
, L.NONINVEN
, L.UOFM
, L.LOCNCODE
, L.UNITPRCE
, L.XTNDPRCE
, L.QUANTITY
, L.ATYALLOC
, L.QTYFULFI
, L.ReqShipDate
, DATEADD(DAY, 2-DATEPART(WEEKDAY, L.ReqShipDate), L.ReqShipDate) as nWkOf
, L.FUFILDAT
, SM.MANUFACTUREORDER_I
, a.xSpiPM
, a.xCustPM
, a.xCeilingType
, a.xCeilingheight
, a.xLatitude
, a.xLongitude
, s.xSignatureRequired
, s.xDrvMilesToStore
, s.xFrtMilesToStore
, s.xGeneralDescription


FROM SOP10200 AS L --ORDER LINES

INNER JOIN SOP10100 AS H --ORDER HEADER
    ON L.SOPTYPE = H.SOPTYPE 
    AND L.SOPNUMBE = H.SOPNUMBE 

INNER JOIN IS010001 AS SM --SOPMOP LINK
    ON L.SOPNUMBE = SM.SOPNUMBE 
    AND L.SOPTYPE = SM.SOPTYPE 
    AND L.LNITMSEQ = SM.LNITMSEQ 
    AND L.CMPNTSEQ = SM.CMPNTSEQ

LEFT JOIN spvCustomerAddr AS A --SP UDF
    ON H.CUSTNMBR = A.Customer_Num 
    AND H.PRSTADCD = A.Address_Code

LEFT JOIN spv3SalesDocument AS S --SP UDF
    ON L.SOPNUMBE = s.Sales_Doc_Num

WHERE
H.VOIDSTTS = 0 
AND L.ITEMNMBR NOT LIKE '1%' 
AND (L.ReqShipDate BETWEEN (@StartDate) and (@EndDate)) 
AND @OrderClass = 
    CASE 
    WHEN s.xGeneralDescription LIKE 'MILLWORK%' THEN 'MillworkOnly'
    ELSE 'NonMillwork'
    END
AND H.SLPRSNID in (@AccountRepName) 
AND H.CUSTNAME in (@CustOrderName) 
AND H.docid = @OrderType AND L.QUANTITY <> 0 
AND (L.QUANTITY - L.QTYFULFI) >= (@FulfilledLines);

报告服务器错误=

For more information about this error navigate to the report server on the local server machine, or enable remote errors ---------------------------- Query execution failed for dataset 'DataSet1'. (rsErrorExecutingCommand) ---------------------------- An error has occurred during report processing. (rsProcessingAborted)

1 个答案:

答案 0 :(得分:1)

  

编辑 - 新建议

好的 - 如果这在报告中不起作用

AND @OrderClass = 
    CASE 
    WHEN s.xGeneralDescription LIKE 'MILLWORK%' THEN 'MillworkOnly'
    ELSE 'NonMillwork'
    END 

报告可能在开始时不喜欢参数 - 它显然不介意它在后端。

由于裸参数启动了比较条款,似乎正在发生。

我想知道你是否可以换一种方式

怎么样

AND 
    (SELECT 
        CASE 
        WHEN s.xGeneralDescription LIKE 'MILLWORK%' THEN 'MillworkOnly'
        ELSE 'NonMillwork'
        END
    ) IN (@OrderClass)