Results 1 to 7 of 7

Thread: [RESOLVED] Difference

Threaded View

  1. #6
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Difference

    Quote Originally Posted by szlamany
    Could you show a sample please of how you start the UDF off, so that we can see how you indicate a return value of a TABLE?
    This is a function we use to calculate daily work load based on a job system we have by transforming hours assigned between a start and end date into hours per day. I have removed the actual T-SQL code since that would be meaningless to include.

    VB Code:
    1. CREATE function dbo.fn_CalculateResourceWorkLoad (@ContactId int, @StartEndDateByMonth bit=0)
    2. returns @calendar table([date] datetime primary key,
    3. hours_assigned float default 0,
    4. hours_total float default 0,
    5. no_work_day bit default 0)
    6.  
    7. as
    8. begin
    9.  
    10. -- Execute TSQL here to populate the @calendar table.
    11.  
    12. return
    13. end

    We then use the function in a SELECT statement to calculate work load grouped by month, week, year, quarter etc.
    VB Code:
    1. set datefirst 1
    2.  
    3. select year([date]) as year,datepart(ww,[date]) as week,
    4. round(sum(hours_assigned),1) as hours_assigned,
    5. sum(hours_total) as hours_total,
    6. min([date]) as start_date,
    7. max([date]) as end_date
    8. from dbo.fn_CalculateResourceWorkLoad(@ContactId,0)
    9. group by year([date]),datepart(ww,[date])

    You could have done that same by only using stored procedures, but I thought that using the UDF as a recordset was so elegant that I just had to do it.
    Last edited by kaffenils; Oct 15th, 2005 at 11:58 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width