title: SQL Server - Transpose rows into columns
body: <p>I've searched high and low for an answer to this so apologies if it's already answered!
  I have the following result from a query in SQL 2005:</p>
  
  <pre><code>ID
  
  1234
  
  1235
  
  1236
  
  1267
  
  1278
  </code></pre>
  
  <p>What I want is</p>
  
  <pre><code>column1|column2|column3|column4|column5
  ---------------------------------------
  1234   |1235   |1236   |1267   |1278
  </code></pre>
  
  <p>I can't quite get my head around the pivot operator but this looks like it's going to be involved. I can work with there being only 5 rows for now but a bonus would be for it to be dynamic, i.e. can scale to x rows.</p>
  
  <p>EDIT:
  What I'm ultimately after is assigning the values of each resulting column to variables, e.g.</p>
  
  <pre><code>DECLARE @id1 int, @id2 int, @id3 int, @id4 int, @id5 int
  
  SELECT @id1 = column1, @id2 = column2, @id3 = column3, @id4 = column4, 
  @id5 = column5 FROM [transposed_table]
  </code></pre>