title: Apply Where Clause if Condition Met
body: <p>I want to apply a where clause if a local variable is not null. eg.</p>
  
  <pre><code>declare @controlName nvarchar(250);
  
      SELECT 
          *
      FROM
      (
      SELECT  TOP (100) PERCENT 
          dbo.Blah.PageName, 
          dbo.BlahBlah.ControlName, 
          dbo.BlahBlah.ControlText, 
          'en' AS FriedEgg
      FROM         
          dbo.WebPageText INNER JOIN
          dbo.Blah ON dbo.BlahBlah.WebPageID = dbo.Blah.WebPageID
  
       if @controlName is not null
  
           WHERE dbo.BlahBlah.ControlName = IN(@controlName, not null)
  </code></pre>
  
  <p>So if the control name  variable is not null, apply the where clause</p>