1
CREATE PROCEDURE [dbo].[rpt_Report]
   @Product varchar(20) = NULL,
   @From DateTime = null,
   @To DateTime = null
AS
BEGIN
    SELECT 
       CO.PO, CO.DLVRY,   
       CASE
          WHEN co.CUST_ID = '120' THEN '120' 
          WHEN HDR.PO_NO LIKE 'CL%' THEN 'CL'
          WHEN RED.REU = 'SK' THEN 'STOCK' 
          WHEN RED.REU = 'SM' THEN 'ORTS' 
          WHEN co.ITEM = 'PW%' THEN 'CHAIN' 
          WHEN co.ITEM LIKE 'D%' THEN 'VAN' ELSE 'MISC' 
       END AS PRODUCT 
     FROM   
        co AS co    
     INNER JOIN 
        HDR AS HDR ON co.NUMBER = HDR.NUMBER 
     INNER JOIN 
        RED AS RED ON co.ITEM = RED.ITEM 
     WHERE   
        (co.DLVRY BETWEEN @From AND @To) AND (@Product = PRODUCT)
     ORDER BY
        co.DLVRY DESC
END

実行するとエラーが発生します

メッセージ207、レベル16、状態1、プロシージャrpt_Report、行34
無効な列名'PRODUCT'。

whereCLAUSEのパラメータとしてcaseフィールド値を使用する必要があります。

4

2 に答える 2

1

PRODUCT句でエイリアスを使用しようとしているようですが、使用WHEREできません。したがって、エイリアスを使用するために、クエリをサブクエリでラップできます。

CREATE PROCEDURE [dbo].[rpt_Report]
   @Product varchar(20) = NULL,
   @From DateTime = null,
   @To DateTime = null
AS
BEGIN
    SELECT x.PO, x.DLVRY, x.PRODUCT
    FROM
    (
        SELECT CO.PO, CO.DLVRY,   
           CASE
              WHEN co.CUST_ID = '120' THEN '120' 
              WHEN HDR.PO_NO LIKE 'CL%' THEN 'CL'
              WHEN RED.REU = 'SK' THEN 'STOCK' 
              WHEN RED.REU = 'SM' THEN 'ORTS' 
              WHEN co.ITEM = 'PW%' THEN 'CHAIN' 
              WHEN co.ITEM LIKE 'D%' THEN 'VAN' ELSE 'MISC' 
           END AS PRODUCT 
         FROM  co AS co    
         INNER JOIN HDR AS HDR 
            ON co.NUMBER = HDR.NUMBER 
         INNER JOIN RED AS RED 
            ON co.ITEM = RED.ITEM 
    ) x        
    WHERE (x.DLVRY BETWEEN @From AND @To) 
        AND (@Product = PRODUCT)
    ORDER BY x.DLVRY DESC
END
于 2012-09-27T18:41:06.023 に答える
0

代わりにこれを試してください:

SELECT 
   CO.PO, CO.DLVRY,   
   PRODUCT = CASE
                WHEN co.CUST_ID = '120' THEN '120' 
                WHEN HDR.PO_NO LIKE 'CL%' THEN 'CL'
                WHEN RED.REU = 'SK' THEN 'STOCK' 
                WHEN RED.REU = 'SM' THEN 'ORTS' 
                WHEN co.ITEM = 'PW%' THEN 'CHAIN' 
                WHEN co.ITEM LIKE 'D%' THEN 'VAN' ELSE 'MISC' 
             END 
 FROM   
    co AS co    
 INNER JOIN 
    HDR AS HDR ON co.NUMBER = HDR.NUMBER 
 INNER JOIN 
    RED AS RED ON co.ITEM = RED.ITEM 
 WHERE   
    (co.DLVRY BETWEEN @From AND @To) AND (@Product = PRODUCT)
 ORDER BY
    co.DLVRY DESC

これがお役に立てば幸いです!!

于 2012-09-27T18:20:11.073 に答える