Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL Server: Retry update if fail

SQL Server: Retry update if fail

Scheduled Pinned Locked Moved Database
databasesql-serversysadminannouncement
2 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mou_kol
    wrote on last edited by
    #1

    create table PendingQueue (
    id int not null,
    DueTime datetime not null,
    Payload varbinary(max),
    cnstraint pk_pending_id nonclustered primary key(id));

    create clustered index cdxPendingQueue on PendingQueue (DueTime);
    go

    create procedure usp_enqueuePending
    @dueTime datetime,
    @payload varbinary(max)
    as
    set nocount on;
    insert into PendingQueue (DueTime, Payload)
    values (@dueTime, @payload);
    go

    create procedure usp_dequeuePending
    @batchsize int = 100,
    @retryseconds int = 600
    as
    set nocount on;
    declare @now datetime;
    set @now = getutcdate();
    with cte as (
    select top(@batchsize)
    id,
    DueTime,
    Payload
    from PendingQueue with (rowlock, readpast)
    where DueTime < @now
    order by DueTime)
    update cte
    set DueTime = dateadd(seconds, @retryseconds, DueTime)
    output deleted.Payload, deleted.id;
    go

    Specially see this line set DueTime = dateadd(seconds, @retryseconds, DueTime) How this line instruct sql server to retry the update data again after 10 minute if fail to update data first time. this is not clear to me. please some one explain if you understand properly. thanks

    Richard DeemingR 1 Reply Last reply
    0
    • M Mou_kol

      create table PendingQueue (
      id int not null,
      DueTime datetime not null,
      Payload varbinary(max),
      cnstraint pk_pending_id nonclustered primary key(id));

      create clustered index cdxPendingQueue on PendingQueue (DueTime);
      go

      create procedure usp_enqueuePending
      @dueTime datetime,
      @payload varbinary(max)
      as
      set nocount on;
      insert into PendingQueue (DueTime, Payload)
      values (@dueTime, @payload);
      go

      create procedure usp_dequeuePending
      @batchsize int = 100,
      @retryseconds int = 600
      as
      set nocount on;
      declare @now datetime;
      set @now = getutcdate();
      with cte as (
      select top(@batchsize)
      id,
      DueTime,
      Payload
      from PendingQueue with (rowlock, readpast)
      where DueTime < @now
      order by DueTime)
      update cte
      set DueTime = dateadd(seconds, @retryseconds, DueTime)
      output deleted.Payload, deleted.id;
      go

      Specially see this line set DueTime = dateadd(seconds, @retryseconds, DueTime) How this line instruct sql server to retry the update data again after 10 minute if fail to update data first time. this is not clear to me. please some one explain if you understand properly. thanks

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      It doesn't. It simply updates the DueTime column in the PendingQueue table, and returns the Payload and id columns of the affected rows. There must be some other code which processes and removes the record from the PendingQueue table which you haven't shown.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups