setting a variable to the result of a dynamic stat

  • Ok, I have a table with several column all labeled, column1, column2, etc. I need to loop through them so I have this statement which will loop through and get the first value, then go to colun2, etc

    declare @score int

    declare @stm nchar(500)

    set @score=0

    While @score<=50

    Begin

    Set @score=@score+1

    set @stm='select column' + rtrim(convert(char(2),@score))+' from tablename'

    exec sp_executesql @stm

    if @score>50

    Break

    else

    COntinue

    end

    My question is, how can I stuff this value from the dynamic sql into a variable such as:

    set @newvalue='exec sp_executesql @stm '

    Thanks,

    Eddie

  • Take a look at sp_executesql in BOL, it will show you how you can use it to do just this.

    "Don't roll your eyes at me. I will tape them in place." (Teacher on Boston Public)

Viewing 2 posts - 1 through 1 (of 1 total)

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