Solved

Set variable using dynamic variable name

Posted on 2006-12-01
6
239 Views
Last Modified: 2011-09-20
I am trying to set a variable using a dynamic name and recieving an error:

 SET @FID + CAST(@OPTION_CNT AS VARCHAR(1)) = @col1
0
Comment
Question by:davidpaez
6 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
Comment Utility
this is not possible , if you wish to do this , you need to modify your entire script to use dynamic sql
0
 
LVL 13

Expert Comment

by:Atlanta_Mike
Comment Utility
SET @col1 = @FID + CAST(@OPTION_CNT AS VARCHAR(1))
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
Comment Utility
                           
Atlanta_Mike,
i think he is looking to set something like

SET @Col1 =
SET @Col2 =
.something like this
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
You can use either a table variable or a temp table to simulate an array.  As we all know, sadly there is no direct array support in T-SQL :( .
0
 
LVL 13

Accepted Solution

by:
Atlanta_Mike earned 500 total points
Comment Utility
Actually it looks like he is trying to set @FID1 = @Col1 and @FID2 = col2 based upon an enumerated value of @OPTION_CNT.

What is your purpose david? Maybe we can help find a work around. Scott is right this is an array. A table variable is most likely your best solution.
0
 

Author Comment

by:davidpaez
Comment Utility
what I'm trying to do is assign values based on a results set stored in a cursor (I have other variables declared but I think this is the detail you are looking for).  I think a simple loop would also work but thought this way would be more effective.

DECLARE csrName CURSOR FOR
SELECT t1.request_id, t1.vocab, t1.menu_option
FROM tbl_f_fulfillment_def t1, tbl_f_fulfillment_def t2
WHERE t2.menu_option <= t1.menu_option and t1.active = 1
group by t1.menu_option, t1.request_id, t1.vocab

SELECT @OPTION_CNT = COUNT(*) FROM TBL_F_FULFILLMENT_DEF WHERE active = 1

OPEN csrName
FETCH NEXT FROM csrName INTO @col1, @col2, @col3
WHILE @@FETCH_STATUS = 0
BEGIN

           --SET @col1 = '@FID + (CAST(@OPTION_CNT AS VARCHAR(1)))' =
          -- SET @VOCAB2 = @col2
          -- SET '@OPTION['@OPTION_CNT']' = @col3

      --    @OPTION_CNT = @OPTION_CNT + 1

FETCH NEXT FROM csrName INTO @col1, @col2, @col3
END --WHILE
CLOSE csrName
DEALLOCATE csrName
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

771 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

14 Experts available now in Live!

Get 1:1 Help Now