title: Generate column list in SQL Server
body: <p>I've cooked up the following SQL to generate the <code>select</code> portion of a query that, given a table, lists all columns in said table:</p>
  
  <pre><code>declare @cols nvarchar(max);
  set @cols = '';
  
  SELECT @cols += lower(', ' + c.name)
  FROM sys.columns c
  WHERE c.object_id = OBJECT_ID('tablename')
  
  select RIGHT(@cols, LEN(@cols) - 2);
  
  -- output:
  -- id, sex, isactive, displayname ...
  </code></pre>
  
  <p>Is there a better way to do this? Are there any tools that can perform this task quickly?</p>
  
  <p>I'm trying to prevent having to write out a bunch of column names. Would much rather generate a list, as described in the code snippet, and remove columns I'm not going to need.</p>