SQL Server Selecting DISTINCT record to string variable
<p>I want to select all distinct name from person table to <code>VARCHAR</code> variable .
I have write a query that returns all name as follow.</p>

<pre><code>DECLARE @pName as VARCHAR(MAX)
SET @pName=''

SELECT @pName += RTRIM(FullName) + ',' 
FROM Persons 

SELECT @pName
</code></pre>

<p>When I try to select distinct on <code>FullName</code>, SQL Server throws an exception.</p>

<pre><code>DECLARE @pName as VARCHAR(MAX)
SET @pName=''

SELECT DISTINCT FullName, @pName += RTRIM(FullName) + ',' 
FROM Persons 

SELECT @pName
</code></pre>

<blockquote>
  <p>Msg 141, Level 15, State 1, Line 3<br>
  A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.</p>
</blockquote>

<p>Distinct on variable name return only first Name</p>

<pre><code>SELECT DISTINCT @pName += RTRIM(FullName) +
FROM Persons 

SELECT @pName
</code></pre>

<p>How can I select distinct name to string variable from <code>SELECT</code> statement?</p>

<p>Thanks in advance</p>