I have a more massive sp, in which I use some temporary tables, below is a part of the sp where I am updating a field in a temporary table two times.
Update #BalSheet
set OBCr = OBDr + isnull(trialDeb.amount,0)
from (
Select v.AccountId,
v.CompanyId,
Sum(isnull(v.Amount,0)) Amount
From #vwJournalDetails v , #TempJournalHeader h
where v.JournalCredit = 1 and // 1
h.JournalId = v.JournalId and
h.CompanyId = v.CompanyId and
h.JournalDate >= @dtPrevFinUnclosedSDate and
h.JournalDate <= @dtLastFinUnclosedEDate
group by v.CompanyId, v.AccountId
) trialDeb
Where trialDeb.Accountid = #BalSheet.AccId and
trialDeb.CompanyId = #BalSheet.CompanyID and
trialDeb.CompanyId = @intCompanyId and
#BalSheet.AcType ='L'
Update #BalSheet
set OBCr = OBDr - isnull(trialDeb.amount,0)
from (
Select v.AccountId,
v.CompanyId,
Sum(isnull(v.Amount,0)) Amount
From #vwJournalDetails v , #TempJournalHeader h
where v.JournalCredit = 0 and // 2
h.JournalId = v.JournalId and
h.CompanyId = v.CompanyId and
h.JournalDate >= @dtPrevFinUnclosedSDate and
h.JournalDate <= @dtLastFinUnclosedEDate
group by v.CompanyId, v.AccountId
) trialDeb
Where trialDeb.Accountid = #BalSheet.AccId and
trialDeb.CompanyId = #BalSheet.CompanyID and
trialDeb.CompanyId = @intCompanyId and
#BalSheet.AcType ='L'
As you can see, the two update query looks similar, and the only difference is in the where condition checking v.JournalCredit value.
If the v.JournalCredit = 1 I add the amount to the current value in the table, and when the v.JournalCredit = 0 I subtract the amount from the current value.
it is working correctly, but there are other update queries similar to this in the sp, So I was wondering if there is any way to mix these update query into one?