Is there a way to SELECT and UPDATE rows at the same time?

后端 未结 9 1510
醉话见心
醉话见心 2020-12-12 21:01

I\'d like to update a set of rows based on a simple criteria and get the list of PKs that were changed. I thought I could just do something like this but am worried about po

相关标签:
9条回答
  • 2020-12-12 21:21

    One way to handle this is to do it in a transaction, and make your SELECT query take an update lock on the rows selected until the transaction completes.

    BEGIN TRAN
    
    SELECT Id FROM Table1 WITH (UPDLOCK)
    WHERE AlertDate IS NULL;
    
    UPDATE Table1 SET AlertDate = getutcdate() 
    WHERE AlertDate IS NULL;
    
    COMMIT TRAN 
    

    This eliminates the possibility that a concurrent client updates the rows selected in the moment between your SELECT and your UPDATE.

    When you commit the transaction, the update locks will be released.

    Another way to handle this is to declare a cursor for your SELECT with the FOR UPDATE option. Then UPDATE WHERE CURRENT OF CURSOR. The following is not tested, but should give you the basic idea:

    DECLARE cur1 CURSOR FOR
      SELECT AlertDate FROM Table1 
      WHERE AlertDate IS NULL
      FOR UPDATE;
    
    DECLARE @UpdateTime DATETIME
    
    SET @UpdateTime = GETUTCDATE()
    
    OPEN cur1;
    
    FETCH NEXT FROM cur1;
    
    WHILE @@FETCH_STATUS = 0
    BEGIN
    
      UPDATE Table1 AlertDate = @UpdateTime
      WHERE CURRENT OF cur1;
    
      FETCH NEXT FROM cur1;
    
    END
    
    0 讨论(0)
  • 2020-12-12 21:25

    Perhaps something more like this?

    declare @UpdateTime datetime
    
    set @UpdateTime = getutcdate()
    
    update Table1 set AlertDate = @UpdateTime where AlertDate is null
    
    select ID from Table1 where AlertDate = @UpdateTime
    
    0 讨论(0)
  • 2020-12-12 21:29

    It'd be easier to do your UPDATE first and then run 'SELECT ID FROM INSERTED'.

    Take a look at SQL Tips for more info and examples.

    0 讨论(0)
提交回复
热议问题