jay-are
asked on
Stored Procedure quirks - Help!
I have problems with an existing stored procedure that just recently cropped up with a server change. This is the sproc:
CREATE procedure usp_Comments1130
As
BEGIN
INSERT INTO Comments1130 (ControlNo, SumAmt)
SELECT [Control #], Sum(Amount)
FROM ODS_accounting_Schedule4 Left Join Comments1130 on ODS_accounting_Schedule4.[ Control #] = Comments1130.ControlNo Where ods_accounting_schedule4.[ Account #] = '1130' AND Comments1130.ControlNo Is Null
GROUP BY [Control #]
HAVING Sum(Amount) <> 0
IF @@ERROR <> 0
BEGIN
RAISERROR('Insert into Comments failed', 16, 1)
RETURN -1
END
DELETE FROM Comments1130
WHERE 0 = (
SELECT SUM(Sched4.Amount)
FROM ODS_accounting_Schedule4 Sched4
WHERE Sched4.[Control #] = Comments1130.ControlNo
AND Sched4.[Account #] = '1130'
)
RETURN 0
END
GO
This seemed to be working fine (no reported errors before now) before a recent server change. Now it doesn't actually give me the Sum(Amount) properly. I'll post the data here for the specific control #'s that its fouling up:
Ctrl# Acct# Amount Reference#
24122 1130 -50.0000 013004
24122 1130 -25.0000 032504
24122 1130 -25.0000 041504
24122 1130 500.0000 8470
25819 1130 -1250.0000 0425CASH
25819 1130 -800.0000 022404
25819 1130 -500.0000 032404
25819 1130 2500.0000 1055
26755 1130 250.0000 1073
26755 1130 500.0000 1084
There are only 4 control #'s in this table that have multiple entries. The three above do not actually get sum'd up by the sproc correctly. The only one that does is this one:
454593 1130 -150.0000 0216CASH
454593 1130 1400.0000 8470
I don't understand why it would work for '454593' and not the rest. I need help cleaning this up because my reports based off this are now inaccurate! Help!
Thanks.
CREATE procedure usp_Comments1130
As
BEGIN
INSERT INTO Comments1130 (ControlNo, SumAmt)
SELECT [Control #], Sum(Amount)
FROM ODS_accounting_Schedule4 Left Join Comments1130 on ODS_accounting_Schedule4.[
GROUP BY [Control #]
HAVING Sum(Amount) <> 0
IF @@ERROR <> 0
BEGIN
RAISERROR('Insert into Comments failed', 16, 1)
RETURN -1
END
DELETE FROM Comments1130
WHERE 0 = (
SELECT SUM(Sched4.Amount)
FROM ODS_accounting_Schedule4 Sched4
WHERE Sched4.[Control #] = Comments1130.ControlNo
AND Sched4.[Account #] = '1130'
)
RETURN 0
END
GO
This seemed to be working fine (no reported errors before now) before a recent server change. Now it doesn't actually give me the Sum(Amount) properly. I'll post the data here for the specific control #'s that its fouling up:
Ctrl# Acct# Amount Reference#
24122 1130 -50.0000 013004
24122 1130 -25.0000 032504
24122 1130 -25.0000 041504
24122 1130 500.0000 8470
25819 1130 -1250.0000 0425CASH
25819 1130 -800.0000 022404
25819 1130 -500.0000 032404
25819 1130 2500.0000 1055
26755 1130 250.0000 1073
26755 1130 500.0000 1084
There are only 4 control #'s in this table that have multiple entries. The three above do not actually get sum'd up by the sproc correctly. The only one that does is this one:
454593 1130 -150.0000 0216CASH
454593 1130 1400.0000 8470
I don't understand why it would work for '454593' and not the rest. I need help cleaning this up because my reports based off this are now inaccurate! Help!
Thanks.
ASKER
Yes they are in that table.
Hmm, maybe I don't really understand the problem :-)
Is the problem that the SUM in the Comments1130 table is incorrect?
If so, then did the last run of the code you gave produce the incorrect sum, placing the wrong amount in Comments1130
<or>
was there a change(s) to an existing control # and the *new* sum is not reflected in Comments1130 (but the old sum was correct)?
Is the problem that the SUM in the Comments1130 table is incorrect?
If so, then did the last run of the code you gave produce the incorrect sum, placing the wrong amount in Comments1130
<or>
was there a change(s) to an existing control # and the *new* sum is not reflected in Comments1130 (but the old sum was correct)?
ASKER
You pointed out the problem I didn't know I was having. I need it to update each control # when a new line/amount is added in the sched4 table. :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I feel stupid for this but this works perfectly. Can't believe I even had to ask why it wasn't working... :) Thanks!
24122
25819
26755
already exist in table Comments1130, right? Because if they already the existing SUM() will naturally not be replaced.