fuzzyocelot
Programmer
Is it possible that the code below is causing deadlocks because of how it's written? Meaning, the table being updated is also referenced in a subquery within the update statement. We know deadlocks are occurring on the tables and this stored procedure was captured in an SQL profiler trace. Would rewriting the procedure help? (BTW, I did not write this proc.)
The code below is from a stored procedure running in SQL Server 2005 SP2.
Thanks!
The code below is from a stored procedure running in SQL Server 2005 SP2.
Code:
Declare @intALSRcdId int, @intUserRcdId int
set @intALSRcdId =1
set @intUserRcdId=20
UPDATE tblPSFS
SET decPaidAmt=ISNULL(psfs.decInitPdAmt,0.00)+t.PaidAmt,
decQ1PaidAmt=Q1PaidAmt, intLastUserRcdId = @intUserRcdId
FROM tblPSFS psfs
JOIN
(SELECT psfsal.intPSFSRcdId,
'PaidAmt'=ISNULL(SUM(psfsal.decPaidAmt),0.00),
'Q1PaidAmt'=ISNULL(SUM(CASE psfsal.inyCQuarter WHEN 1 THEN ISNULL(psfsal.decPaidAmt,0.00) ELSE 0.00 END),0.00)
FROM dbo.tblPSFSAL psfsal
WHERE psfsal.intPSFSRcdId in
(select psfs.intPSFSRcdId
from tblPSFS psfs
where coalesce(psfs.intPSSCRcdID,psfs.intPSLICRcdID) in
(select coalesce(als.intPSSCRcdID,als.intPSLICRcdID)
from tblALS als
where als.intALSRcdId=ISNULL(@intALSRcdId,als.intALSRcdId) )
)
GROUP BY psfsal.intPSFSRcdId ) as t
ON t.intPSFSRcdId=psfs.intPSFSRcdId
UPDATE tblFS
SET decPaidAmt=t.PaidAmt+ISNULL(fs.decInitPdAmt,0.00),
decQ1PaidAmt=t.Q1PaidAmt,
intLastUserRcdId = @intUserRcdId
FROM tblFSfs JOIN
(SELECT psfs.intFSRcdId,
'PaidAmt'=SUM(ISNULL(psfsal.decPaidAmt,0.00)),
'Q1PaidAmt'=ISNULL(SUM(CASE psfsal.inyFSQuarter WHEN 1 THEN ISNULL(psfsal.decPaidAmt,0.00) ELSE 0.00 END),0.00)
FROM dbo.tblPSFSAL psfsal JOIN
dbo.tblPSFS psfs ON psfsal.intPSFSRcdId=psfs.intPSFSRcdId
WHERE psfs.intFSRcdId in
(select intFSRcdId from tblPSFS psfs where coalesce(psfs.intPSSCRcdID,psfs.intPSLICRcdID)in
(select coalesce(als.intPSSCRcdID,als.intPSLICRcdID)
from tblALS als where als.intALSRcdId=ISNULL(@intALSRcdId,als.intALSRcdId)))
GROUP BY psfs.intFSRcdId) as t
ON t.intFSRcdId=fs.intFSRcdId
Thanks!