Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

Set variable using dynamic variable name

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
davidpaez
Asked:
davidpaez
1 Solution
 
Aneesh RetnakaranDatabase AdministratorCommented:
this is not possible , if you wish to do this , you need to modify your entire script to use dynamic sql
0
 
Atlanta_MikeCommented:
SET @col1 = @FID + CAST(@OPTION_CNT AS VARCHAR(1))
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
                           
Atlanta_Mike,
i think he is looking to set something like

SET @Col1 =
SET @Col2 =
.something like this
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
Scott PletcherSenior DBACommented:
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
 
Atlanta_MikeCommented:
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
 
davidpaezAuthor Commented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now