ChewDoggie
Programmer
Hello All,
I'm trying to create a function, (I believe I want a scalar-function). It queries a table, performs calculations and then returns the result as an decimal.
Here's the code:
This query will produce several rows of DATA. I want to loop through the rows, perform calculations on the two dates and then return the calculated DATA as a Decimal. It keeps barfing with the error "Select statements included within a function cannot return data to a client". Interesting as I'm not trying to return data from a select statement.
Anyway, any assistance would be appreciated.
Chew
10% of your life is what happens to you. 90% of your life is how you deal with it.
I'm trying to create a function, (I believe I want a scalar-function). It queries a table, performs calculations and then returns the result as an decimal.
Here's the code:
Code:
USE [MySampleDatabase]
GO
/****** Object: UserDefinedFunction [dbo].[GetCalculatedHours] Script Date: 11/23/2009 12:58:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Chewdoggie
-- Create date: 11/23/2009
-- Description:
-- =============================================
ALTER FUNCTION [dbo].[GetCalculatedHours]
(
-- Add the parameters for the function here
@inAttendanceEndDate Date,
@inAssociateGUID UniqueIdentifier,
@inTotalHours Decimal(6,2)
)
RETURNS Decimal(6,2)
AS
BEGIN
-- Declare the return variable here
DECLARE @Switch Integer
DECLARE @DaysDiff Integer
DECLARE @Days Integer
DECLARE @CalcHours Integer
DECLARE @SD Date
DECLARE @ED Date
SET @Switch = 0
SET @DaysDiff = 0
DECLARE dates_cursor CURSOR FOR SELECT AttendanceStartDate, AttendanceEndDate FROM tblAttendance as Att INNER JOIN tblReasons as R on R.ReasonGUID = Att.ReasonGUID where Att.AttendanceEndDate >= DATEADD(DAY, -365, GetDate()) AND Att.AttendanceEndDate <> Att.AttendanceStartDate and R.BonusEffect < 2 and Att.AssociateGUID = @inAssociateGUID and Att.TotalHours > 0.00
OPEN dates_cursor
FETCH NEXT FROM dates_cursor into @SD, @ED
SET @DaysDiff = DATEDIFF(DAY, @SD, @ED) + 1
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @SD as AttendanceStartDate, @ED as AttendanceEndDate
FETCH NEXT FROM dates_cursor into @SD, @ED
SET @DaysDiff += DATEDIFF(DAY, @SD, @ED) + 1
--SET @DaysDiff += DATEDIFF(DAY, Att.AttendanceStartDate, Att.AttendanceEndDate)
END
SET @CalcHours = 0
IF @Days <= (@DaysDiff + 365)
SET @CalcHours = @Days * 8
-- Return the result of the function
RETURN @CalcHours
END
This query will produce several rows of DATA. I want to loop through the rows, perform calculations on the two dates and then return the calculated DATA as a Decimal. It keeps barfing with the error "Select statements included within a function cannot return data to a client". Interesting as I'm not trying to return data from a select statement.
Anyway, any assistance would be appreciated.
Chew
10% of your life is what happens to you. 90% of your life is how you deal with it.