title: "IF..ElseIf..Else" or "Where clause" to guide Stored Procedure results
body: <p>I have the following two SQL statements</p>
  
  <p>First one:</p>
  
  <pre><code>IF(@User_Id IS NULL)  
  BEGIN  
        SELECT *
        FROM [UserTable]
  END  
  ELSE           
  BEGIN                  
        SELECT *
     FROM  [UserTable] AS u
     WHERE  u.[Id] = @User_Id                  
  END 
  </code></pre>
  
  <p>Second one:</p>
  
  <pre><code>SELECT  *
  FROM [UserTable] AS u
  WHERE (@User_Id IS NULL OR u.[Id] = @User_Id)
  </code></pre>
  
  <p>Both of those queries would be wrapped in its own stored procedure. I am suspecting that the IF statement is causing a lot of recompilations on SQL. I am faced with either separating each part of the IF statement into its own stored procedure, OR replacing the entire IF statement with a WHERE clause (illustrated above in the second SQL statement)</p>
  
  <p>My question is: What is the difference between the two statements from a performance perspective, and how would SQL treat each statement?</p>
  
  <p>Thanks.</p>