Hi @Sudip Bhatt ,
You could also try with adding WAITFOR DELAY so that you could loop and check for new rows every several seconds as you defined.
Please refer another similar example from below or here.
-- Create table
CREATE TABLE [dbo].[work_queue](
[work_queue_id] [int] IDENTITY(1,1) NOT NULL,
[name] varchar(255) NOT NULL, -- a simple name for each item
[processed_flag] bit NOT NULL, -- 0: Ready for processing, 1: Processed
CONSTRAINT [PK_work_queue] PRIMARY KEY CLUSTERED
(
[work_queue_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)
-- Add data to table
INSERT INTO work_queue (name, processed_flag)
SELECT 'A', 0
UNION ALL SELECT 'B', 0
UNION ALL SELECT 'C', 0
UNION ALL SELECT 'D', 0
UNION ALL SELECT 'E', 0
UNION ALL SELECT 'F', 0
UNION ALL SELECT 'G', 0
UNION ALL SELECT 'H', 0
UNION ALL SELECT 'I', 0
UNION ALL SELECT 'J', 0
UNION ALL SELECT 'K', 0
DECLARE @work_queue_id int, @name varchar(255)
BEGIN TRANSACTION
-- select 1 queue item to process that hasn't been processed yet
SELECT TOP(1)
@work_queue_id = work_queue_id, @name = name
FROM
work_queue WITH (UPDLOCK, READPAST)
WHERE processed_flag = 0
PRINT 'Processing ' + @name + ', work_queue_id = ' + CONVERT(varchar, @work_queue_id)
-- simulate delay
WAITFOR DELAY '00:00:15'
-- flag item as processed
UPDATE work_queue SET processed_flag = 1 WHERE work_queue_id = @work_queue_id
COMMIT
You could also refer this link and this link for more details about using a table as a queue which may be helpful to you.
Best regards
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.