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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/