Solved

TSQL Variable Reference

Posted on 2011-09-13
4
309 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
  • 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 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