title: SSRS: Reporting services with a stored procedure that returns columns with same name
body: <p>I'm in a situation where we're converting reports from Crystal Report to SSRS (Sql Server Reporting Services).</p>
  
  <p>The original report was provided results from a stored procedure which returned columns with the same exact name.</p>
  
  <p>How this is possible? I explain you:</p>
  
  <p>The previous programmer did some horrible hack. It put a select result into a #TempTable. </p>
  
  <p>Let's assume that the select to stuff #TempTable does</p>
  
  <pre><code>SELECT a,b,c INTO #TempTable FROM mytable 
  </code></pre>
  
  <p>Then at the end of the stored procedure this is what is returned:</p>
  
  <pre><code>SELECT aliasTemp.*, a, b, c from #TempTable aliasTemp
  </code></pre>
  
  <p>The result returned is</p>
  
  <p>a,b,c,a,b,c</p>
  
  <p>This is HORRIBLE in terms of SQL but I can't change database as my customer doesn't want any change to be made to the production environment.</p>
  
  <p>When I try to add this stored procedure as a datasource to the report I get this:</p>
  
  <p><a href="https://i.stack.imgur.com/MZqyD.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MZqyD.jpg" alt="Error"></a></p>
  
  <p>Which is a self explanatory error which says that while reporting services tries to retrieve the fields that the stored procedure returns, if finds duplicate named columns so it won't work.</p>
  
  <p>Since Reporting services allows you to use both sql queries or stored procedures, what I tought could be a solution would be doing this query directly into the report as a SQL SELECT</p>
  
  <pre><code>CREATE TABLE #tmpBus
  (
     a INT,
     b INT,
     c INT
  )
  
  INSERT INTO #tempTable
  Exec StupidStoredProcedure'Params'
  </code></pre>
  
  <p>Avoiding the extra columns retrieved which have the same name and extracting only the first 3.</p>
  
  <p>But it says the extracted columns doesn't match the temp table.</p>
  
  <p>How I can achieve this?</p>