Solved

TSQL Variable Reference

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Help in SQL Full Text Search 3 28
Log Backup 2 13
Sql query to Stored Procedure 6 20
BULK INSERT most recent CSV 19 21
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now