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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL Server Simultaneous Update with WHERE EXISTS

SQL Server Simultaneous Update with WHERE EXISTS

Scheduled Pinned Locked Moved Database
databasesharepointsql-serversysadminbusiness
2 Posts 1 Posters 0 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.
  • C Offline
    C Offline
    c0der2009
    wrote on last edited by
    #1

    This is a SQL Server query (I am using SQL Server 2008). I have an application which runs jobs. I have a JOB table with columns JOB_ID, JOB_STEP etc. When a job is started, an entry is made in this table. When each step is completed, this entry is updated to reflect the next step in JOB_STEP column. The application is a multi processing application, ie, there are multiple instances of the same application running on multiple servers. All of them read, insert and update into the same database table. (There is only one instance of the DB). Now, I have a business requirement that no two jobs should be on step 20 (or step 100) together. Ie, if any one job is already on step 20 or 100, all other jobs should wait till that job step is completed. I have a stored procedure which the application calls to update the step info. I tried something like update JOB set JOB_STEP = @nextStep where ( (@nextStep <> '20' AND @nextStep <> '100') OR ( (@nextStep = '20' OR @nextStep = '100') AND NOT EXISTS ( select * from LAUNCH_STATISTICS where (JOB_STEP = '20' OR JOB_STEP = '100') and JOB_ID <> @jobId ) ) ) and JOB_ID = @jobId; But it didn’t work. If jobs try to change steps one after the other, this works. But if I call this SP from two different processes at the same time, both of them manage to end up with Step 20 (or 100) sometimes. Please help me in finding out what I am missing. I tried with different TRANSACTION_ISOLATION levels but it didn't help.

    C 1 Reply Last reply
    0
    • C c0der2009

      This is a SQL Server query (I am using SQL Server 2008). I have an application which runs jobs. I have a JOB table with columns JOB_ID, JOB_STEP etc. When a job is started, an entry is made in this table. When each step is completed, this entry is updated to reflect the next step in JOB_STEP column. The application is a multi processing application, ie, there are multiple instances of the same application running on multiple servers. All of them read, insert and update into the same database table. (There is only one instance of the DB). Now, I have a business requirement that no two jobs should be on step 20 (or step 100) together. Ie, if any one job is already on step 20 or 100, all other jobs should wait till that job step is completed. I have a stored procedure which the application calls to update the step info. I tried something like update JOB set JOB_STEP = @nextStep where ( (@nextStep <> '20' AND @nextStep <> '100') OR ( (@nextStep = '20' OR @nextStep = '100') AND NOT EXISTS ( select * from LAUNCH_STATISTICS where (JOB_STEP = '20' OR JOB_STEP = '100') and JOB_ID <> @jobId ) ) ) and JOB_ID = @jobId; But it didn’t work. If jobs try to change steps one after the other, this works. But if I call this SP from two different processes at the same time, both of them manage to end up with Step 20 (or 100) sometimes. Please help me in finding out what I am missing. I tried with different TRANSACTION_ISOLATION levels but it didn't help.

      C Offline
      C Offline
      c0der2009
      wrote on last edited by
      #2

      In short, Is UPDATE-WHERE-EXISTS able to handle concurrency? If not, then is there any way to get that done?

      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