Link to home
Start Free TrialLog in
Avatar of ffowler1
ffowler1

asked on

TSQL Variable Reference

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.
Avatar of JestersGrind
JestersGrind
Flag of United States of America image

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

Avatar of ffowler1
ffowler1

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of JestersGrind
JestersGrind
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It was a great starting point from where I could complete the rest on my own.
Thank you very much.