Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

 

I have a C# program that calls a SP (MSSQL 2000) and I want my SP to first SELECT some rows and then UPDATE ONLY those selected rows. As my SP will run for quite a long time, I want my SP to

 

allow other users to insert rows while the SP is running

but I don't want

 

  • other users to modify the rows I have selected
  • to update rows that are inserted between my SELECT and UPDATE

 

Here is my SP (simplified version)

 

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ

/* Store the required rows in a table variable */

DECLARE @SelectedID TABLE (ID char (10) NOT NULL PRIMARY KEY)

INSERT INTO @SelectedID
SELECT ID FROM
tableA INNER JOIN tableB ON ... WHERE ....

/* Select all required rows from @SelectedID and
this will be return as a datatable to my C# program */

SELECT fieldA,... FROM tableA INNER JOIN tableB
ON ... where ID IN (SELECT ID FROM @SelectedID)

/* Update ONLY selected rows */

UPDATE tableA SET fieldA = 'whatever' WHERE ID IN (SELECT ID FROM @SelectedID)

COMMIT TRAN

The problem with this SP is other users cannot insert rows while the SP is running (they need to wait until the SP finishes). What isolation level and lock type should I use?

 

Thanks in advance.

 

Michael

There is no spoon. <<The Matrix>>

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...