Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2004
    Location
    Edmonton, Canada
    Posts
    72

    Unanswered: Construct Variable Name?

    Can you contruct a variable name from another variable? For example, I want the following to PRINT 10

    Code:
    DECLARE @var1 INT
    DECLARE @var2 INT
    
    SET @var1 = 10
    SET @var2 = 20
    
    PRINT '@var' + '1'
    This prints the variable name, not the contents of the var. I tried to parse it with square brackets, but no luck.

    Thanks,
    Carl

  2. #2
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    You can do this using dynamic SQL, though I'm not sure it would work for the specific PRINT statement in your example.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  3. #3
    Join Date
    Jul 2004
    Location
    Edmonton, Canada
    Posts
    72
    Ya, I need a varName in a var. A datatype of VARNAME. in some langs you just parse it correctly and it works, some it won't.

    if you substitute EXEC for PRINT, it says you must declare variable 'var1', as you can see it obviously is being declared and interpretted the way it was typed.


    Carl

  4. #4
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Actually, dynamic SQL will not work for this example, because the scope of the variables is limited to the procedure in which they are created.

    Please explain what you are trying to do, and maybe we can come up with a satisfactory solution.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  5. #5
    Join Date
    Jul 2004
    Location
    Edmonton, Canada
    Posts
    72
    I am just trying to simplify the code.

    In stead of:
    Code:
    IF @var1 < @var2
        SET @var3 = DATEADD(n,GETDATE(), @var1)
    ELSE
        SET @var3 = DATEADD(n,GETDATE(), @var2)
    I was hoping to simply:
    Code:
        SET @var3 = DATEADD(n,GETDATE(), @var + @CurrentVarIndex)
    I already know which one I want, @var1 or @var2, it's just that I have to IF and have 2 lines to do the work of 1.

  6. #6
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Quote Originally Posted by carlmal
    ... it's just that I have to IF and have 2 lines to do the work of 1.
    I've seen worse. Using dynamic SQL or some other hack work around to do this is going to end up making your code more complicated, not simpler.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  7. #7
    Join Date
    Jul 2004
    Location
    Edmonton, Canada
    Posts
    72
    I know what you mean regarding the dynamic sql and "the sea of red". can make reviewing your code quite miserable.
    Carl

Posting Permissions

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