title: Select all if parameter is null in stored procedure
body: <p>I want to create a procedure in SQL Server that will select and join two tables. The parameters @company, @from and @to are always set but @serie_type can be NULL. If @serie_type is not NULL i just want to include the specified types, simple <code>AND S.Type = @serie_type</code>, but if @serie_type is NULL i want to include all types, simple, just dont include the AND statement. My problem is that i dont know if @serie_type will be set therefore i would like o have something like this:</p>
  
  <pre><code>/* pseudocode */
  ??? = AND (IF @serie_type IS NOT NULL S.Type = @serie_type)
  </code></pre>
  
  <p>Here is a simpifyed version of procedure:</p>
  
  <pre><code>CREATE PROCEDURE Report_CompanySerie
      @company    INT,
      @serie_type INT,
      @from       DATE,
      @to         DATE
  AS
  BEGIN
      SELECT
          *
      FROM Company C
          JOIN Series S ON S.Company_FK = C.Id
      WHERE C.Id = @company 
          AND S.Created &gt;= @from
          AND S.Created &lt;= @to
  /* HERE IS MY PROBLEM */        
          AND ???
  END
  GO
  </code></pre>
  
  <p>Don't want to duplicate the select becaust the real select is way bigger then this.</p>