Select rows and Update same rows for locking?

前端 未结 2 1564
不思量自难忘°
不思量自难忘° 2021-01-21 15:02

I need to write a procedure that will allow me to select x amount of rows and at the same time update those rows so the calling application will know those records are locked an

2条回答
  •  春和景丽
    2021-01-21 15:50

    As you suggested, you can use the OUTPUT clause effectively:

    Live demo: https://data.stackexchange.com/stackoverflow/query/8058/so3319842

    UPDATE #tbl
    SET locked = 1
    OUTPUT INSERTED.*
    WHERE id IN (
        SELECT TOP 1 id
        FROM #tbl
        WHERE locked = 0
        ORDER BY id
    )​
    

    Also see this article:

    http://www.sqlmag.com/article/tsql3/more-top-troubles-using-top-with-insert-update-and-delete.aspx

提交回复
热议问题