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

    How to update 2 rows at a time by a single update command

    eno ename sal
    1 'a' 100
    2 'b' 200

    here i want to update a employee names as a 'z' and 'r' based on empid by using single update command,please give me solution to me
  • #746745
    Hi,

    Use the below SQL Script


    UPDATE table_1
    SET ename = CASE
    WHEN eno = 1 THEN 'z'
    WHEN eno = 2 THEN 'r'
    END

    Regards
    Siva

  • #746749
    Hi Lakshmireddy,

    To update two rows at a time we can use case statement.


    create table #emp (Eno int,EName varchar(10),Sal int)
    insert into #emp values(1,'a',100),(2,'b',200)
    select * from #emp

    update #emp set EName=case when Eno=1 then 'z' when Eno=2 then 'r' end

  • #746775
    Hai Lakshmi,
    Since SQL Server 2008, the single insert command can be used to insert multiple records.
    But i don't think there is any such things for update command as well.
    But you can use the Case statement to do that.
    As per your scenario, you can use the below query:

    Table:
    eno ename sal
    1 'a' 100
    2 'b' 200
    Query:
    UPDATE Emp
    SET ename =
    CASE
    WHEN eno = 1 THEN 'z'
    WHEN eno = 2 THEN 'r'
    END

    Hope it will be helpful to you.

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

  • #747289
    You can update N rows at any one time...there is no limit. If you want to update any 2 specific rows then you have to identify those rows by id.


  • Sign In to post your comments