Home Forums SQL Server 2005 SQL Server Newbies How to add two column and the to put the result on the next row? RE: How to add two column and the to put the result on the next row?

  • ashishjain (2/6/2013)


    Hi angjoni,

    You can use below code to perform this operation:

    create table test99 (column1 int, column2 int, result int)

    insert into test99

    values

    (50,1500,0),

    (100,0,0),

    (40,0,0),

    (30,0,0)

    declare @col2 int = 1500

    select * from test99

    update test99

    set @col2 = result = @col2 + column1,

    column2 = @col2

    from test99

    select * from test99

    drop table test99

    This is the quirky update method. Very fast indeed, but you must adhere to some rules to garantuee consistent behaviour. The article Sean linked to has a very good description of the method and its rules.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP