• vikramchander90 (10/18/2012)


    Exact code used is this

    Create proceduretest1

    As

    Begin

    Declare@col1totalint

    Declare@col2totalint

    Declare@col3totalint

    Declare @dynqrynvarchar(max)

    Declare @counterint

    Select@counter=1

    While@counter<=3

    Begin

    Select@dynqry='Declare @col'+convert(varchar(5),@counter)+'total int'

    Select@col'+CONVERT(varchar(5),@counter)+'total=SUM(col'+CONVERT(varchar(5),@counter)+') FROM Table1'

    exec sp_executesql @dynqry

    Select @counter=@counter+ 1

    End

    Select @col1total,@col2total,@col3total

    End

    But even if i declare and assign value after executing the query the output value is showing as null

    Why all this hassle?

    This will do exactly the same:

    Create proceduretest1

    As

    begin

    select sum(col1), sum(col2), sum(col3) from Table1

    end

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]