title: How can I choose Union ALL according to passed parameter ( need optimization )?
body: <p>Say we have a stored procedure that take <code>@IsAllowed</code> parameter which is boolean value and in case it passed <code>True</code> I should select data from two tables <code>A,B</code> ( I'll use in my case <code>Union ALL</code> ) Else it passed <code>False</code> I should select data From one table <code>A</code> ... I write it int he following way : </p>
  
  <pre><code>Create PROCEDURE TestSP
  (
      @IsAllowed bit
  )
  AS
  
  IF @IsAllowed = 1
  BEGIN
      Select ID, Username From A
      Union ALL
      Select ID, Username From B
  END
  ELSE
      Select ID, Username From A
  </code></pre>
  
  <p><strong>Is it the best way to do that ?</strong> although in my real case SP have about 9 parameter used in where condition of First query, that's mean i'll write the first query twice and any change in it I must take care to have the same copy in the 2 different places</p>