Solved

how to return dynamic column name in stored procedure?

Posted on 2013-01-07
7
253 Views
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
END

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
0
Comment
Question by:hongclub
  • 4
  • 3
7 Comments
 
LVL 19

Expert Comment

by:Rimvis
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.

DECLARE @sSQL NVARCHAR(MAX)
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 ...'
			
EXEC (@sSQL)
         

Open in new window

0
 

Author Comment

by:hongclub
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)
BEGIN
DECLARE @sSQL NVARCHAR(MAX)
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 '

                  
EXEC (@sSQL)
END


(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
0
 

Author Comment

by:hongclub
ID: 38755963
any suggestion, can't believe it is so complicated to generate a dynamic column name
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 19

Accepted Solution

by:
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?
0
 

Author Comment

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

Author Comment

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

Expert Comment

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to use left join to take all data from master table? 11 61
ORA-00923: FROM keyword not found where expected 3 66
SQL Server syntax question 13 32
Mysql Left Join Case 10 55
In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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