title: Need help in generating Query
body: <p>I have a doubt in generating Query.</p>
  
  <p>Which is best way in these two</p>
  
  <p>Eg:1</p>
  
  <pre><code>DECLARE @SQLQuery varchar(MAX)
  SET @SQLQuery='Select tab1.Name,tab2.Name From table1 INNER JOIN table2 ON table2.ID=table1.ID'
  
  IF @Val=0
   SET @SQLQuery=@SQLQuery+' where table1.ID&gt;5'
  ELSE
   SET @SQLQuery=@SQLQuery+' where table2.ID&gt;5'
  </code></pre>
  
  <p>Eg 2:</p>
  
  <pre><code>IF @Val=0
  BEGIN
  Select tab1.Name,tab2.Name 
  From table1 
  INNER JOIN table2 
      ON table2.ID=table1.ID
  WHERE table1.ID&gt;5
  END
  ELSE
  BEGIN
  Select tab1.Name,tab2.Name 
  From table1 
  INNER JOIN table2 
      ON table2.ID=table1.ID
  WHERE table2.ID&gt;5
  END
  </code></pre>