hold_semaphore


IF EXISTS (SELECT * FROM sysobjects 
           WHERE id = OBJECT_ID('dbo.hold_semaphore') 
           AND sysstat & 0xf = 4)
	DROP PROCEDURE dbo.hold_semaphore
GO

CREATE PROCEDURE hold_semaphore
     @name VARCHAR(30) = 'textreader'
AS
  /* wait until the requested semaphore 
     is available then grab it */

SET NOCOUNT ON

WHILE (SELECT status FROM semaphore
       WHERE name = @name) = 1
  BEGIN
    WAITFOR DELAY "00:00:10"    
  END

  UPDATE semaphore
  SET status = 1
  WHERE name = @name
  AND status = 0		 

GO