get value of variable

  • hi, plz execute above code in Query analyzer.

    declare @year char(4)

    Declare @visa varchar(25)

    declare @visa1985 varchar(25)

    set @visa ='56'

    Set @year ='1985'

    select @visa = '@visa' + ''+@year+''

    Print @visa

    Exec('select '''+@visa+'''')

    It will display @visa1985 as output but instead i want the value of @visa1985

    to be printed. Can any one help me how to do that? Thanks

  • Is this what you are looking for?

    select @visa = @visa + @year --note removed '

    Print @visa

    SET @visa1985 = 'select '''+@visa+''''

    PRINT @Visa1985

    Returns:

    561985

    select '561985'

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • ummm not really. i have years starting from 1985 to 2009 and it will increase every year so next year 2010 will be added.

    @visa1985 is a parameter like wise we have parameters upto @visa2009. And all these parameters have different values that user will pass from SP.

    I am trying to minimize my code here thats why i m dynamically selecting @visa1985, @visa1986 from my while loop but i want actual value of @visa1985 to be displayed.

    I hope u got what i want to tell. Thanks

  • dallas13

    Sorry, but I am rather dense, and do not understand your amplified explanation. Some items is question:

    1. Does user pass in a single parameter value?

    2. If more than one parameter value, must values for each declared parameter be supplied?

    3. You have posted this question to a SQL 2005 forum and yet want to run the code in Query Analyzer which is a SQL 2000 interface. Which is it 2000 ot 2005?

    How about posting your T-SQL code in its entirety and maybe some one can and will assist you further.

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • declare @year char(4)

    Declare @visa varchar(25)

    declare @visa1985 varchar(25)

    set @visa ='56'

    Set @year ='1985'

    --select @visa = '@visa' + ''+@year+''

    --Print @visa

    --

    --Exec('select '''+@visa+'''')

    SELECT @visa = @visa+@Year

    SELECT @visa

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Sorry, but I am rather dense, and do not understand your amplified explanation. Some items is question:

    1. Does user pass in a single parameter value?

    --no users can pass any number of parameters. They can pass @visa1985 =int, @visa1990 =null,

    @visa1995 =int.means they can pass values and if they don’t want to then its null

    2. If more than one parameter value, must values for each declared parameter be supplied?

    3. You have posted this question to a SQL 2005 forum and yet want to run the code in Query Analyzer which is a SQL 2000 interface. Which is it 2000 ot 2005?

    --its sql 2005 question.

    I am still not able to get what I want from above code. I want @visa1985 = 56 or any int value that can be supplied and I don’t want @visa1985 = 561985. I hope I m able to explain it properly.

  • declare @year char(4)

    Declare @visa varchar(25)

    declare @visa1985 varchar(25)

    Set @year ='1985'

    set @visa ='56'

    set @visa = '@visa' + ''+@year+'' +'=' + @visa

    select @visa

    | If in Doubt...don't do it!! |

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply