Closed Thread
Results 1 to 3 of 3

Thread: Change variable name within loop

  1. #1
    LiLMsNinja's Avatar
    LiLMsNinja is offline Learning Programmer
    Join Date
    May 2008
    Posts
    42
    Rep Power
    0

    Change variable name within loop

    Soo.. is there any way in the world to change a variable name within a loop using the number stored in the count variable. Similiar to:

    Code:
    SELECT @CNT = 1
    WHILE @CNT <= 3
    BEGIN
    
    -- THE LVL1 VARIABLE NEEDS TO HAVE THE NUMBER 1 CHANGE AS
    -- THE NUMBER STORED WITHIN @CNT CHANGES
    
    SELECT @LVL = LVL1 FROM @FINALSECTION
    	WHERE ROWNUM = @RowCnt
    
    SELECT @STR = @STR + (SELECT hierarchy_number FROM	DATABASE.dbo.Hierarchy
                 WHERE hierarchy_id = @LVL)
    
    
    SELECT @STR = @STR + (SELECT hierarchy_NAME FROM	DATABASE.dbo.Hierarchy
                WHERE hierarchy_id = @LVL)
    
    
    SELECT @CNT = @CNT + 1
    END
    I've tried numerous combinations and nothing is working.

    I would also like to put a dash between each 'hierarchy_number' and 'hierarchy_name' that i pull. Similiar logic as:

    Code:
    SELECT @STR = @STR + (SELECT hierarchy_number FROM 			DATABASE.dbo.Hierarchy					WHERE hierarchy_id = @LVL) + ' - '
    
    
    SELECT @STR = @STR (SELECT hierarchy_NAME FROM 				DATABASE.dbo.Hierarchy					WHERE hierarchy_id = @LVL) + ' - '
    But that gives an 'Incorrect syntax near ' - '. But only for the second hyphen. WTF!!
    Last edited by LiLMsNinja; 09-25-2009 at 02:35 PM. Reason: Stupid formatting.
    CodeMonkey +15 | CommunicationSkills -34 | ClarityUsingEnglish -55 | Clarity while pointing at the monitor and making vigorous facial expressions, occasional grunts, and mouth clicks +150

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    LiLMsNinja's Avatar
    LiLMsNinja is offline Learning Programmer
    Join Date
    May 2008
    Posts
    42
    Rep Power
    0

    Re: Change variable name within loop

    haha.. i'm missing a ' + ' in the second @STR... Where i grab the 'hierarchy_name'. Wow..

    But, still need to figure out how to increment the variable
    CodeMonkey +15 | CommunicationSkills -34 | ClarityUsingEnglish -55 | Clarity while pointing at the monitor and making vigorous facial expressions, occasional grunts, and mouth clicks +150

  4. #3
    zeroradius's Avatar
    zeroradius is offline Speaks fluent binary
    Join Date
    Feb 2008
    Location
    Ohio
    Posts
    1,403
    Rep Power
    25

    Re: Change variable name within loop

    have you tried seting the veriable name to a veriable and then concantanating cnt on to it than using that veriable in place of the veriable you want to have change in the query?

    I don't even know what language that is your using but to illistrate what i mean i will show you in php with mysql.

    Code:
    $i=1;
    while(
    $i<=5)
      {
      
    $newVar "oldVar" $i;
       
    $blah "Select * from $newvar";
      
    $i++;
      }
    ?> 
    The above syntex won't run but should give you anidea of what i am talking about.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 11-26-2010, 08:53 AM
  2. Initializing Variable Within a Loop
    By sourlemon in forum General Programming
    Replies: 1
    Last Post: 04-10-2010, 07:48 AM
  3. loop:{ if(true) {goto:loop;}}//PHP 5.3
    By BlaineSch in forum PHP Development
    Replies: 22
    Last Post: 09-12-2009, 06:07 AM
  4. Using input to change a variable?
    By Plutonic in forum Java Help
    Replies: 3
    Last Post: 07-17-2007, 02:02 PM
  5. Change Resolution, Change Size
    By Void in forum C# Programming
    Replies: 1
    Last Post: 06-13-2006, 12:48 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts