how to return dynamic column name in stored procedure?

Posted on 2013-01-07
Last Modified: 2013-01-09
in my stored procedure, i group the result set to return multiple groups like 'Sheet1', 'Sheet2'

Here is my the grouping return in my SP:
select [Sheet + @loop] = 'Sheet' + Convert(varchar(10), @loop), * from @Result result
WHERE result.State = (Select top 1 State from @Group where RowID = @loop) AND
      SELECT @loop = @loop + 1

How can i make the first column name as 'Sheet1', 'Sheet2', 'Sheet3', etc.
I know my syntax is wrong as [Sheet + @loop], but just want to give u some idea
Question by:hongclub
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 19

Expert Comment

ID: 38754592
Hi hongclub,

You will have to use dynamic SQL for that. Please see example bellow. If parameters for you query are coming from external sources, make sure you are validating it against SQL injection attacks.

SET @sSQL = N'select [Sheet' + Convert(varchar(10), @loop) +'] = ''Sheet' + Convert(varchar(10), @loop)+ ', * from ' + @Result + ' result '
			+ N'WHERE result.State = (Select top 1 State from ' + @Group + ' where RowID = ' + Convert(varchar(10), @loop)+ ') AND ...'

Open in new window


Author Comment

ID: 38755534
thanks, i tired it, however there are two issues i am facing

(1) it returned me error, it said 'Must declare the scalar variable "@Result", but i have that @Result table declare also, so i twist my code to this
WHILE (@loop < = (Select count(*) from @Group))
-- WHILE (@loop < = 2)
SET @sSQL = N'select [Sheet' + Convert(varchar(10), @loop) +'] = ''Sheet' + Convert(varchar(10), @loop)+ ', * from @Result result '
                  + N'WHERE result.State = (Select top 1 State from @Group ' + ' where RowID = ' + Convert(varchar(10), @loop)+ ') AND '


(2) after i change, the sp is so slow, it took 10 seconds to run, and eventually i stop executing the SP, is it because it is in the while loop

Author Comment

ID: 38755963
any suggestion, can't believe it is so complicated to generate a dynamic column name
Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

LVL 19

Accepted Solution

Rimvis earned 500 total points
ID: 38758006
So @Result is a table variable? You have to change it to temporary table to use it in dynamic SQL. Besides that, temp tables generally are performing better with large (>1000 records) datasets.

Also, I don't see @loop incremented anywhere. Could it be you are running an infinite loop?

Author Comment

ID: 38759864
thanks Rimvis, i got it working using #Result dynamic table

Author Comment

ID: 38759869
another question, can i create dynamic table with dynamic column
LVL 19

Expert Comment

ID: 38761250
What do you mean? Could you give an example please?

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question