Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to return dynamic column name in stored procedure?

Posted on 2013-01-07
7
Medium Priority
?
260 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
[X]
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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 19

Accepted Solution

by:
Rimvis earned 2000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

636 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