Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

select for update Expand / Collapse
Author
Message
Posted Wednesday, December 12, 2012 8:49 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, July 28, 2014 7:25 AM
Points: 4, Visits: 12
Hello,

I have a problem with selecting rows which I want to update.
Select sets a read lock on the rows and then if I try to update selected rows I get a deadlock.

In ORACLE there is a SELECT FOR UPDATE clause but it does not exist in the MSSQL. How it should be solved?
In easy words how to select rows which then I want to update in the same transaction properly?

Thank you in advance for help.
Post #1395716
Posted Wednesday, December 12, 2012 8:50 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, August 28, 2014 7:14 AM
Points: 167, Visits: 682
Please provide us with the code you use for your update.

Based on what you are writing just now, I assume you use a correlated subquery?

B

Post #1395719
Posted Wednesday, December 12, 2012 8:57 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, July 28, 2014 7:25 AM
Points: 4, Visits: 12
Hi,

I think you will not be able to find the reason without knowing the tables structure.
We've changed select to update already and added rowlock but it does not work. Deadlocks are on the first update

However, I am attaching the problematic transaction:

begin tran
set @inseason = 0

--check if we fit into existing range

select
@indateto = dateto
,@inseason = seasonid
from
tblSeasonDates
where
datefrom<@datefrom
and
dateto>@dateto
and
seasonid in (select seasonid from tblSeason where groupid=@groupid)


if @inseason = 0
begin
--both ends are in different ranges
print 'update tblSeasonDates 1'
--shorten first
update
tblSeasonDates WITH (ROWLOCK)
set
DateTo=@DateFrom,
user_modified = @user_modified
where
DateTo > @DateFrom
and
DateFrom < @DateFrom
and
SeasonID in (select seasonid from tblSeason where groupid=@groupid)

print 'update tblSeasonDates 2'
--shorten second
update
tblSeasonDates WITH (ROWLOCK)
set
Datefrom=@DateTo,
user_modified = @user_modified
where
DateTo > @DateTo
and
DateFrom <= @DateTo
and
SeasonID in (select seasonid from tblSeason where groupid=@groupid)

print 'delete tblSeasonDates 1'
--delete overlapped
delete
from
tblSeasonDates
where
DateFrom>=@DateFrom
and
DateTo<=@DateTo
and
SeasonID in (select seasonid from tblSeason where groupid=@groupid)

print 'insert tblSeasonDates 1'
--insert new
Insert into tblSeasonDates
(SeasonID, DateFrom , dateTo, user_modified)
values
(@SeasonID, @DateFrom , @DateTo, @user_modified)

end
else
begin
--we are inside range

print 'update tblSeasonDates 3'
--shorten first part
update
tblSeasonDates WITH (ROWLOCK)
set
DateTo=@DateFrom,
user_modified = @user_modified
where
DateTo = @indateto
and
SeasonID in (select seasonid from tblSeason where groupid=@groupid)

print 'insert tblSeasonDates 2'
--insert second part
Insert into tblSeasonDates
(SeasonID, DateFrom , dateTo, user_modified)
values
(@inseason, @DateTo , @indateto, @user_modified)

print 'insert tblSeasonDates 3'
--insert new
Insert into tblSeasonDates
(SeasonID, DateFrom , dateTo, user_modified)
values
(@SeasonID, @DateFrom , @DateTo, @user_modified)
end

print 'delete tblSeasonDates 2'

commit tran
Post #1395728
Posted Wednesday, December 12, 2012 9:10 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, August 28, 2014 7:14 AM
Points: 167, Visits: 682
Why don't you take the initial select statement out of the transaction all together? ... just keep the update statements in the transaction if you need it.

HTH,

B
Post #1395743
Posted Thursday, December 13, 2012 4:39 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, August 28, 2014 7:14 AM
Points: 167, Visits: 682
so? ... did the suggestion work?

B
Post #1396120
Posted Thursday, December 13, 2012 7:05 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, July 28, 2014 7:25 AM
Points: 4, Visits: 12
Unfortunately no. We are still looking for a solution...
Post #1396174
Posted Thursday, December 13, 2012 9:40 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, August 29, 2014 2:32 PM
Points: 13,302, Visits: 12,168
You do realize that this query will not produce consistent results?

select
@indateto = dateto
,@inseason = seasonid
from
tblSeasonDates
where
datefrom<@datefrom
and
dateto>@dateto
and
seasonid in (select seasonid from tblSeason where groupid=@groupid)

If there is more than 1 row returned from this query, your variables will contain the values from the last row in the result set. Of course you have no order by so you don't know what the last row will be.

I don't think you need a correlated subquery for this. I am pretty sure this query will give you the same results.

select @indateto = dateto,
@inseason = seasonid
from tblSeasonDates sd
join tblSeason s on s.seasonid = sd.seasonid
where datefrom < @datefrom
and dateto > @dateto
and s.groupid = @groupid

I would drop your ROWLOCK query hint. Query hints in SQL server can be very detrimental and should only be used when you are absolutely certain that the hint is the best choice. Lock escalation in sql server is very complex and unless you understand it really well it is best to avoid this.

Also I used a free online sql formatter (http://poorsql.com) to format your code so it is easier to read.



BEGIN TRANSACTION

SET @inseason = 0

--check if we fit into existing range
SELECT @indateto = dateto
,@inseason = seasonid
FROM tblSeasonDates
WHERE datefrom < @datefrom
AND dateto > @dateto
AND seasonid IN (
SELECT seasonid
FROM tblSeason
WHERE groupid = @groupid
)

IF @inseason = 0
BEGIN
--both ends are in different ranges
PRINT 'update tblSeasonDates 1'

--shorten first
UPDATE tblSeasonDates
WITH (ROWLOCK)

SET DateTo = @DateFrom
,user_modified = @user_modified
WHERE DateTo > @DateFrom
AND DateFrom < @DateFrom
AND SeasonID IN (
SELECT seasonid
FROM tblSeason
WHERE groupid = @groupid
)

PRINT 'update tblSeasonDates 2'

--shorten second
UPDATE tblSeasonDates
WITH (ROWLOCK)

SET Datefrom = @DateTo
,user_modified = @user_modified
WHERE DateTo > @DateTo
AND DateFrom <= @DateTo
AND SeasonID IN (
SELECT seasonid
FROM tblSeason
WHERE groupid = @groupid
)

PRINT 'delete tblSeasonDates 1'

--delete overlapped
DELETE
FROM tblSeasonDates
WHERE DateFrom >= @DateFrom
AND DateTo <= @DateTo
AND SeasonID IN (
SELECT seasonid
FROM tblSeason
WHERE groupid = @groupid
)

PRINT 'insert tblSeasonDates 1'

--insert new
INSERT INTO tblSeasonDates (
SeasonID
,DateFrom
,dateTo
,user_modified
)
VALUES (
@SeasonID
,@DateFrom
,@DateTo
,@user_modified
)
END
ELSE
BEGIN
--we are inside range
PRINT 'update tblSeasonDates 3'

--shorten first part
UPDATE tblSeasonDates
WITH (ROWLOCK)

SET DateTo = @DateFrom
,user_modified = @user_modified
WHERE DateTo = @indateto
AND SeasonID IN (
SELECT seasonid
FROM tblSeason
WHERE groupid = @groupid
)

PRINT 'insert tblSeasonDates 2'

--insert second part
INSERT INTO tblSeasonDates (
SeasonID
,DateFrom
,dateTo
,user_modified
)
VALUES (
@inseason
,@DateTo
,@indateto
,@user_modified
)

PRINT 'insert tblSeasonDates 3'

--insert new
INSERT INTO tblSeasonDates (
SeasonID
,DateFrom
,dateTo
,user_modified
)
VALUES (
@SeasonID
,@DateFrom
,@DateTo
,@user_modified
)
END

PRINT 'delete tblSeasonDates 2'

COMMIT TRANSACTION




_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1396268
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse