Solved

How to process records which are not locked in master table.

Posted on 2014-04-09
3
291 Views
Last Modified: 2014-04-11
Hi,
Here is a scenario to which I need good solution.

I keep getting messages into a table ABC (24 hours & 7 days a week). Every message contains unique ID and that goes into ID column of ABC table.

This same ID is also in another master table XYZ.

Now I have a Win service which processes the messages (24 hours & 7 days a week).

Win service selects top 1 unprocessed ID from ABC table and call the SP p_proc1 to process the message. p_procc1 takes ID as argument. Once the messge is processed it will mark the rcord as processed (sets the column 'processed' to 'Y')

Now the issue is - SP p_proc1 cannot process the messages if the record with same ID in XYZ table has been locked by some application. (Some UI application locks the records indefinitely for hours and sometimes even days.)

So while getting the ID from ABC table – I want to make sure it is NOT locked (rowlock) in XYZ table. How to do this?

Another question is - how to process continuously the messages in ABC table in database itself without Win Service? Can anyone suggest me the solution how to process the messages without Win service (I mean in database itself) 24/7 ?

If the record is locked in master table XYZ, it should process the message later when it is unlocked.- We are not sure when the record will be unlocked in XYZ table. Sometimes it may take 10 days to get unlocked.

I will be thanful if someone suggests me a good a solution for this.
0
Comment
Question by:GouthamAnand
3 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 39989283
I don't know how you could get a records that is "not locked", unless this is defined in some column (like LOCKED='Y') status ...
if it's locked like you said using that other table, continue reading

so, for the processing part, you could create a sql agent job run do the same as the winservice:
declare @id int
select top 1 @id = id from ABC where not exists(select null from xyz where xyz.id = abc.id ) and ( processed is null or processed <> 'Y' )
while @@rowcount <> 0
begin
  exec yourproc @id

  select top 1 @id = id from ABC where not exists(select null from xyz where xyz.id = abc.id ) and ( processed is null or processed <> 'Y' )

end

Open in new window


hope this helps
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 250 total points
ID: 39989837
For performance reasons, I'd create a filtered index on the table.  

CREATE UNIQUE INDEX ABC__IX_Not_Processed ON dbo.ABC ( id )
    INCLUDE ( processed, locked )
    WHERE processed <> 'Y' AND locked <> 'Y'
    WITH ( FILLFACTOR = 99, ONLINE = ON, SORT_IN_TEMPDB = ON ) --change as needed
    ON [PRIMARY] --change as needed
   

Then to process:

DECLARE @id int
WHILE 1 = 1
BEGIN
    SELECT TOP (1) @id = id
    FROM dbo.ABC
    WHERE processed <> 'Y' AND locked <> 'Y' --<<-- exactly same conditions as in "create index"
    IF @@ERROR > 0 OR @@ROWCOUNT = 0
        BREAK
    EXEC dbo.p_proc1 @id
END --WHILE



CREATE UNIQUE INDEX ABC__IX_Not_Processed ON dbo.ABC ( id ) INCLUDE ( processed, locked )
0
 

Author Closing Comment

by:GouthamAnand
ID: 39994446
Thanks for you both. I like both the answers.
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now