You must Sign In to post a response.
  • Category: SQL Server

    Update Statement in SQL Server

    Hi,

    I have a sql table in below format

    No A1 A2 A3 A4 A5
    1 100 0 0 0 0
    2 101 0 0 0 0
    3 102 0 0 0 0
    4 103 0 0 0 0
    5 104 0 0 0 0
    6 105 0 0 0 0

    I want to update A2,A3,A4,A5 values as follows
    A2=A1+2
    A3=A2+3
    A4=A3+4
    A5=A4+5

    I want a single update statement for above scenario


    Output Table
    No A1 A2 A3 A4 A5
    1 100 102 105 109 114
    2 101 103 106 110 115
    3 102 104 107 111 116
    4 103 105 108 112 117
    5 104 106 109 113 118
    6 105 107 110 114 119
  • #763959
    You try simple update statement as follows

    update tableName set A2=A1+2, A3=(A1+5), A4 = A1+9, A5 = A1+15

    By Nathan
    Direction is important than speed

  • #763967
    Agree with Nathan post, need any help let us know.
    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #763994
    Hai Sivs,
    Yes, you can write the simple Update query for the update in the column values as:

    UPDATE <table_name>
    SET
    A2= A1+2,
    A3= A2+3,
    A4= A3+4,
    A5 = A4+5

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments