Solved

Set variable using dynamic variable name

Posted on 2006-12-01
6
291 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
[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
6 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 18055384
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
ID: 18055427
SET @col1 = @FID + CAST(@OPTION_CNT AS VARCHAR(1))
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 18055441
                           
Atlanta_Mike,
i think he is looking to set something like

SET @Col1 =
SET @Col2 =
.something like this
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 18055538
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
ID: 18055966
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
ID: 18056425
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

623 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