Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Stored Proc Help

Author  Topic 

bmahony993
Yak Posting Veteran

58 Posts

Posted - 2008-01-18 : 17:38:37
I have a stored procedure, and the user enters a projectId, start date and end date. The sp does some calculations and also works out a beginning balance. However, if the user wants to use a wildcard for the projectId, I don't know how to make the "beginBal" calculation for each project. Below is part of the code I have. Any help would be greatly appreciated :)

Create Table #tbl_BalanceSheet (projectId char(6),
project_desc char(100),
customer char(10),
projSite char(10),
status_pa char(10),
rpm char(5),
rpmName char(50),
padm char(5),
padmName char(50),
begin_date char(12),
end_date char(12),
JrnlType char(4),
TranType char(3),
TranDate char(12),
PerEnt char(10),
RefNbr char(10),
batNbr char(10),
perpost char(6),
acct char(4),
trandesc char(100),
beginBal money,
cramt money,
dramt money
)


INSERT INTO #tbl_BalanceSheet(projectId, project_desc, customer,projSite, status_pa, rpm, rpmName, padm, padmName, begin_date, end_date, JrnlType, TranType,
TranDate, PerEnt, RefNbr, batNbr, perpost, acct, trandesc, beginBal,cramt, dramt)
SELECT g.projectId, p.project_desc, p.customer, NULL, p.status_pa, p.manager1, NULL, p.manager2, NULL, p.start_date, p.end_date, g.JrnlType, g.TranType, g.TranDate, g.PerEnt,
g.RefNbr, g.batNbr, g.perpost, g.acct, g.trandesc,0, g.cramt, g.dramt
FROM GLTran g INNER JOIN
PJPROJ p ON
g.projectid = p.project
WHERE g.acct = 1400 and g.posted = 'p'
AND (g.projectId LIKE @project_ID + '%')

Update #tbl_BalanceSheet
Set beginBal = (Select Sum([dramt]-[cramt])from #tbl_BalanceSheet where perpost < @startPeriod),
padmName = (Select formalName from tblAbraData where pen = padm),
rpmName = (Select formalName from tblAbraData where pen = rpm),
projSite = (Select distinct pe_id02 from PJPENT where project LIKE @project_ID)

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-18 : 22:01:20
quote:
Originally posted by bmahony993

I have a stored procedure, and the user enters a projectId, start date and end date. The sp does some calculations and also works out a beginning balance. However, if the user wants to use a wildcard for the projectId, I don't know how to make the "beginBal" calculation for each project. Below is part of the code I have. Any help would be greatly appreciated :)

Create Table #tbl_BalanceSheet (projectId char(6),
project_desc char(100),
customer char(10),
projSite char(10),
status_pa char(10),
rpm char(5),
rpmName char(50),
padm char(5),
padmName char(50),
begin_date char(12),
end_date char(12),
JrnlType char(4),
TranType char(3),
TranDate char(12),
PerEnt char(10),
RefNbr char(10),
batNbr char(10),
perpost char(6),
acct char(4),
trandesc char(100),
beginBal money,
cramt money,
dramt money
)


INSERT INTO #tbl_BalanceSheet(projectId, project_desc, customer,projSite, status_pa, rpm, rpmName, padm, padmName, begin_date, end_date, JrnlType, TranType,
TranDate, PerEnt, RefNbr, batNbr, perpost, acct, trandesc, beginBal,cramt, dramt)
SELECT g.projectId, p.project_desc, p.customer, NULL, p.status_pa, p.manager1, NULL, p.manager2, NULL, p.start_date, p.end_date, g.JrnlType, g.TranType, g.TranDate, g.PerEnt,
g.RefNbr, g.batNbr, g.perpost, g.acct, g.trandesc,0, g.cramt, g.dramt
FROM GLTran g INNER JOIN
PJPROJ p ON
g.projectid = p.project
WHERE g.acct = 1400 and g.posted = 'p'
AND (g.projectId LIKE @project_ID + '%')

Update #tbl_BalanceSheet
Set beginBal = (Select Sum([dramt]-[cramt])from #tbl_BalanceSheet where perpost < @startPeriod),
padmName = (Select formalName from tblAbraData where pen = padm),
rpmName = (Select formalName from tblAbraData where pen = rpm),
projSite = (Select distinct pe_id02 from PJPENT where project LIKE @project_ID)






Change Update like this & try:-


Update t
Set t.beginBal = (Select Sum([dramt]-[cramt])from #tbl_BalanceSheet where projectId=t.projectId AND perpost < @startPeriod),
padmName = (Select formalName from tblAbraData where projectId=t.projectId AND pen = padm),
rpmName = (Select formalName from tblAbraData where projectId=t.projectId AND pen = rpm),
projSite = (Select distinct pe_id02 from PJPENT where project = t.projectId )
FROM #tbl_BalanceSheet t
Go to Top of Page

bmahony993
Yak Posting Veteran

58 Posts

Posted - 2008-01-22 : 10:56:37
That almost works, but seems to only return the beginBal for the first projectId - any others in the dataset are getting a null value?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-22 : 11:29:55
quote:
Originally posted by bmahony993

That almost works, but seems to only return the beginBal for the first projectId - any others in the dataset are getting a null value?


Try this then:-
Create Table #tbl_BalanceSheet (projectId char(6),
project_desc char(100),
customer char(10),
projSite char(10),
status_pa char(10),
rpm char(5),
rpmName char(50),
padm char(5),
padmName char(50),
begin_date char(12),
end_date char(12),
JrnlType char(4),
TranType char(3),
TranDate char(12),
PerEnt char(10),
RefNbr char(10),
batNbr char(10),
perpost char(6),
acct char(4),
trandesc char(100),
beginBal money,
cramt money,
dramt money
)


INSERT INTO #tbl_BalanceSheet(projectId, project_desc, customer,projSite, status_pa, rpm, rpmName, padm, padmName, begin_date, end_date, JrnlType, TranType,
TranDate, PerEnt, RefNbr, batNbr, perpost, acct, trandesc, beginBal,cramt, dramt)
SELECT g.projectId, p.project_desc, p.customer, NULL, p.status_pa, p.manager1, NULL, p.manager2, NULL, p.start_date, p.end_date, g.JrnlType, g.TranType, g.TranDate, g.PerEnt,
g.RefNbr, g.batNbr, g.perpost, g.acct, g.trandesc,0, g.cramt, g.dramt
FROM GLTran g INNER JOIN
PJPROJ p ON
g.projectid = p.project
WHERE g.acct = 1400 and g.posted = 'p'
AND (g.projectId LIKE @project_ID + '%')

Update tbs
Set tbs.beginBal = Sum(CASE WHEN perpost < @startPeriod THEN tbs.[dramt]-tbs.[cramt] ELSE 0 END),
tbs.padmName = tad1.formalName ,
tbs.rpmName = tad2.formalName ,
tbs.projSite = p.pe_id02
FROM #tbl_BalanceSheet tbs
INNER JOIN tblAbraData tad1
ON tad1.pen =tbs.padm
INNER JOIN tblAbraData tad2
ON tad2.pen =tbs.rpm
INNER JOIN PJPENT p
ON p.project=tbs.projectid
Go to Top of Page

bmahony993
Yak Posting Veteran

58 Posts

Posted - 2008-01-22 : 12:22:03
That doesn't work either - "an aggregate may not appear in the set list of an update statement". I tried moving the SUM into the CASE statement but still got the same error :(
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-22 : 12:26:51
quote:
Originally posted by bmahony993

That doesn't work either - "an aggregate may not appear in the set list of an update statement". I tried moving the SUM into the CASE statement but still got the same error :(


Can you explain what the value beginBal stores?
Go to Top of Page

bmahony993
Yak Posting Veteran

58 Posts

Posted - 2008-01-22 : 12:38:03
It is a data type Money, and should ideally add the credit column(cramt)for each month before the start period that the user enters, and subtract that total from the sum of the debit column (dramt) for each month before the start period.

project cramt dramt perpost
133500 25 50 200612
133500 14 0 200611
133500 25 60 200610
133500 26 40 200701

If the user entered 200701 as the start date, then the beginning balance would be (150 - 90) or 60. I need to calculate this for each project, assuming the user will use a wild card something like 1335%
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-22 : 12:41:34
try like this:-
t.beginBal = (Select Sum([dramt])-Sum([cramt])from #tbl_BalanceSheet where projectId=t.projectId AND perpost < @startPeriod)
Go to Top of Page

bmahony993
Yak Posting Veteran

58 Posts

Posted - 2008-01-22 : 12:47:28
That still only gives me a beginning balance for the first project, with NULL for the others that follow. I wonder if it just can't be done this way at all...Thanks for all your suggestions though :)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-22 : 12:49:15
quote:
Originally posted by bmahony993

That still only gives me a beginning balance for the first project, with NULL for the others that follow. I wonder if it just can't be done this way at all...Thanks for all your suggestions though :)


Are you sure you dont have NULL values in both the fields?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-22 : 12:56:01
try this too:-
Update tbs
Set tbs.beginBal = tmp.OpenBal,
tbs.padmName = tad1.formalName ,
tbs.rpmName = tad2.formalName ,
tbs.projSite = p.pe_id02
FROM #tbl_BalanceSheet tbs
INNER JOIN tblAbraData tad1
ON tad1.pen =tbs.padm
INNER JOIN tblAbraData tad2
ON tad2.pen =tbs.rpm
INNER JOIN PJPENT p
ON p.project=tbs.projectid
INNER JOIN (SELECT projectid,Sum([dramt])-Sum([cramt]) AS OpenBal
FROM #tbl_BalanceSheet
WHERE perpost < @startPeriod
GROUP BY projectid)tmp
ON tmp.projectid=tbs.projectid
Go to Top of Page

bmahony993
Yak Posting Veteran

58 Posts

Posted - 2008-01-22 : 13:00:11
There is definitely a value in cramt and dramt - even if the value is 0. I will have to check the data some more - there must be something I am not seeing that is causing the NULL for some values. Again, thanks so much for your help!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-22 : 13:06:07
quote:
Originally posted by bmahony993

There is definitely a value in cramt and dramt - even if the value is 0. I will have to check the data some more - there must be something I am not seeing that is causing the NULL for some values. Again, thanks so much for your help!


tried my last attempt too?
Go to Top of Page
   

- Advertisement -