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

    Query to select & update rows in sql

    hi

    i want to select the records where flag=0 and at same time update record to flag=1...this will show tht record has been fetched nd need not to fetch it again...

    how to do this..ie select statements nd update them at same time..in sql server
  • #348234
    Hi,

    Your question is not clear. You mean, you want to update all the records whose flag = 0? If so,

    UPDATE TABLENAME
    SET FLAG = 1
    WHERE FLAG = 0

  • #348236
    no..i said i want to select record ie fetch nd update them at same thime

    is this possible

  • #348239
    wirte a Stored procedure and put two commands
    like
    Create proc pr
    (
    @id varchar(50)
    }
    as
    select * from employee where id=@id
    update employee set flag = 1 where id=@id
    end

  • #348372
    Hi,
    The subQuery fetches the data that have flag as 0 value.The result of the Subquery is updated to have flag value as 1.
    I hope that i am going with your demand.

    Query
    UPDATE Test SET Flag=1
    WHERE ID IN(SELECT ID FROM Test WHERE Flag=0 )

    Note:Here ID is a primary key of the table you are using.


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.