title: SQL Server 2008 - Select distinct names into variable
body: <p>I need to send emails from SQL Server, to a constantly changing list of names.
  Luckily, the names are updated by users into a SQL Server table, so it should always be up to date.</p>
  
  <p>I have already researched, and am hoping to use <code>sp_SQLNotify</code> to send the emails.
  (This is already set up and tested with actual lists of names - NOT yet with a variable).</p>
  
  <p>Anyway, I need to populate a variable with the list of email addresses to send to.</p>
  
  <p>In order to do this, I need the equivalent of:</p>
  
  <pre><code>SELECT DISTINCT [Email_Username] + '@my_email_suffix.com; '
  INTO @VARIABLE
  FROM My_Table
  
  EXEC sp_SQLNotify @Variable 'My Header' 'My wall of text'
  </code></pre>
  
  <p>Is this possible, and what is the true syntax that I need to follow?</p>
  
  <p>Please forgive my ignorance, but I struggle with some of the syntax.</p>
  
  <p>Thanks, Craig.</p>
  
  <h1>===== UPDATED WITH ANSWER =====</h1>
  
  <p>Thanks to Aaron.</p>
  
  <p>I used your code as the basis for a working solution.</p>
  
  <p>For anyone trying to do this in future, the final result is:</p>
  
  <pre><code>-- Declare variable and populate with initial apostrophe
  DECLARE @var VARCHAR(MAX) = '''';
  
  -- Populate variable with all unique email user names
  SELECT @var += x.email
  FROM 
  (
    SELECT DISTINCT [Email_Username] + '@my_email_suffix.com;'  AS email
    FROM dbo.[My_Table]
    WHERE [Email_Username] &lt;&gt; ''
  ) AS x 
  
  -- Remove final semi-colon
  SET @var = left(@var,len(@var)-1)
  
  -- Add final apostrophe
  SET @var = @var + ''''
  
  -- Display result
  SELECT @VAR;
  </code></pre>