Solved

TSQL Variable Reference

Posted on 2011-09-13
4
324 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 500 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

695 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