title: SQL Server - Set a variable if a value from one table exists in another
body: <p>I have tables that are set up as shown below:</p>
  
  <pre><code>Table 1:  
  ID      Name     Date
  1       a         2000-01-01
  2       b         2001-01-01
  3       c         2002-01-01`
  
  Table 2:
  ID    Name
  2       b
  3       c   
  </code></pre>
  
  <p>I would like to return all in Table 1 and then have a column that will hold a variable of either 'Yes' or 'No', based on whether they exist in Table 2, as shown below. </p>
  
  <p>As shown:</p>
  
  <pre><code>Results:
  
  Name   Date         Yes/No
    a    2000-01-01    No
    b    2001-01-01    Yes    
    c    2002-01-01    Yes
  </code></pre>
  
  <p>I have:</p>
  
  <pre><code>DECLARE @boolean as varchar(10)
  IF EXISTS(
  SELECT ID FROM Table 2
  )
  SET @boolean = 'Yes'  
  ELSE SET @boolean = 'No'
  
  SELECT Name, Date, @boolean as 'Yes/No'
  FROM Table 1
  LEFT JOIN Table 2 u ON Table 1.ID = Table 2.ID
  </code></pre>
  
  <p>However, this returns the results as shown below:</p>
  
  <pre><code>Name   Date         Yes/No
    a    2000-01-01    Yes
    b    2001-01-01    Yes    
    c    2002-01-01    Yes
  </code></pre>
  
  <p>Any ideas on how to manipulate this query to return what is expected?  </p>