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

x
?
Solved

TSQL Variable Reference

Posted on 2011-09-13
4
Medium Priority
?
336 Views
Last Modified: 2012-05-12
Hello All,

Have a problem that I am hoping someone could help with. I have  stored procedure where I am assigning column values to individual variables as in the following manner

@c0 = Column A
@c1 = Column B
@c2 = Column C .... etc

In a while loop I want to reference the variables, singularly, but without naming each variable so I tried to construct a pointer (in a sense):
SELECT @Component = '@c' + CONVERT(nvarchar(5), @tCounter)
This however gives me the string literal "@c1" or "@c2" (or whatever @tCounter is at the moment)
So my question is, is it possible to refer to a variable without having to actually typing it out.
To be clearer, I am trying to flatten out a row into individual rows, per column.
Thank you all for your considerations and time.
0
Comment
Question by:ffowler1
[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
  • 2
  • 2
4 Comments
 
LVL 21

Expert Comment

by:JestersGrind
ID: 36531378
You should probably use a table variable for what you are trying to do.  

DECLARE @c TABLE(RowID INT,  Value NVARCHAR(5))

Then in your loop assign the value like this.

SELECT @Component = Value FROM @c WHERE RowID = @tCounter.

Greg

0
 

Author Comment

by:ffowler1
ID: 36535549
Greg,

Thanks for the time answering, but I must have not been as clear as I should have been. I am actually already grabbing a row for a table, I want to access the fields within that row so that I may insert the individiual fields on it's own separate row in another table.

Thoughts anyone?
0
 
LVL 21

Accepted Solution

by:
JestersGrind earned 1500 total points
ID: 36535842
I still think that there is probably a set based solution, but I don't know enough about the code to suggest one.  To do what you are trying to do, you have to use dynamic SQL.  Take a look at this code.

DECLARE @SQL NVARCHAR(500)

Then inside your loop.

SET @SQL = 'SELECT @Component = @c' + CONVERT(nvarchar(5), @tCounter)

EXECUTE sp_executesql @SQL

You dynamically create the variable assignment and then execute it.

Greg

0
 

Author Closing Comment

by:ffowler1
ID: 36718756
It was a great starting point from where I could complete the rest on my own.
Thank you very much.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

604 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