title: Using Case field as parameter column in stored procedures in SQL Server
body: <pre><code>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
  </code></pre>
  
  <p>When I execute it I am getting an error saying </p>
  
  <blockquote>
    <p>Msg 207, Level 16, State 1, Procedure rpt_Report, Line 34<br>
    Invalid column name 'PRODUCT'.</p>
  </blockquote>
  
  <p>I should use the case field value as parameter in where CLAUSE.</p>