Solved

# Syntax help with SQL Function

Posted on 2011-03-06
344 Views
I have a funtion with one parameter that returns a sum of all wages paid for a particular job from a table called TimeSheets. I use this funtion in a stored procedure but now I find I need fringe costs (Union Dues) also. For every row of wages there are four lines of fringe costs in another related table that I need to include as part of the total wages.

Here is the function I use to return labor costs against a job:

ALTER FUNCTION [dbo].[fn_LaborCosts]
(@JobNumber char(5))
RETURNS MONEY
AS
BEGIN
DECLARE @TotalAmount money
SELECT @TotalAmount = (SELECT sum(ts.LaborAmount)
FROM      TimeSheets      ts

WHERE ts.JobNumber = @JobNumber)

RETURN @TotalAmount

Here is how I use it in the Stored procedure:

ALTER PROCEDURE [dbo].[spSelectJobs]
AS
SELECT
JobNumber,
JobDescription,
ProposalPrice,
dbo.fn_LaborCosts(j.JobNumber) AS TotalLabor

FROM   dbo.Jobs   j

The PK for the Fringe table is TsKey so for every row in the TimeSheets table there are four rows in the Fringe table with the TsKey. I need to add up all the fringe costs and add them to the total labor costs and I am not sure how to accomplish this. Thanks for any help on this.
0
Question by:AkAlan
• 2
• 2

LVL 40

Accepted Solution

Sharath earned 250 total points
ID: 35050113
Alter the function like this.
``````ALTER FUNCTION [dbo].[FN_LABORCOSTS]
(@JobNumber CHAR(5))
RETURNS MONEY
AS
BEGIN
DECLARE  @TotalAmount MONEY

SELECT @TotalAmount = SUM(ts.LaborAmount + ISNULL(f.FringeCost,0))
FROM TimeSheets ts
LEFT JOIN (  SELECT TsKey,
SUM(FringeCost) FringeCost
FROM Fringe
GROUP BY TsKey) f
ON ts.TsKey = f.TsKey
WHERE ts.JobNumber = @JobNumber

RETURN @TotalAmount
END
``````
0

LVL 75

Assisted Solution

Anthony Perkins earned 250 total points
ID: 35052154
Or just simply (no points please):
``````ALTER FUNCTION [dbo].[FN_LABORCOSTS] (@JobNumber CHAR(5))

RETURNS MONEY

AS

BEGIN

RETURN (
SELECT   SUM(ts.LaborAmount + ISNULL(f.FringeCost, 0))
FROM     TimeSheets ts
LEFT JOIN (SELECT   TsKey,
SUM(FringeCost) FringeCost
FROM     Fringe
GROUP BY TsKey) f ON ts.TsKey = f.TsKey
WHERE    ts.JobNumber = @JobNumber
)
END
``````
0

LVL 75

Expert Comment

ID: 35063539
Sharath_123,

My apologies, as you can see I did try, unfortunately it seems the author overlooked my request.

Feel free to object.

Anthony
0

LVL 6

Author Comment

ID: 35070776
Ace, I just realized what happened here. I will try and rectify it. I thought the second post was you with a cleaner solution than your first.
0

LVL 40

Expert Comment

ID: 35071490
acperkins -  Not an issue. :) Expected a split, but that's ok.
0

## Featured Post

### Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…