Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

table-valued function

$
0
0

okay I'm a bit stuck, I get this error:

The last statement included within a function must be a return statement.

My code:

-- ================================================
-- Template generated from Template Explorer using:
-- Create Multi-Statement Function (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the function.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE FUNCTION ResultsUnder20daysCompMsp
(
	@startDate datetime,
	@endDate datetime,
	@Division varchar(70)
)
RETURNS @ResultsUnder20days table
(
	RefNo varchar(60), 
	IssueType varchar(90), 
	TheDivision varchar(60), 
	ConsentFormReceivedThroughDoor datetime, 
	CallCompleteDate datetime
)
AS
	begin
	declare
	 @hols int,
	 @DayAllowance int,
	 @Days int	

	set @DayAllowance = 20
	set @hols = (select hols from(select count(*) as Hols from tbl_holidays where (TheDate between @StartDate and @EndDate)) as hols)

(		
	select RefNo, IssueType, TheDivision, ConsentFormReceivedThroughDoor, CallCompleteDate, 
	(DATEDIFF(dd, ConsentFormReceivedThroughDoor, CallCompleteDate) + 1) -- Number of days between Start and End
					-
						(
							(DATEDIFF(wk, ConsentFormReceivedThroughDoor, CallCompleteDate)*2) --Weekend Days+(case when DATENAME(dw, ConsentFormReceivedThroughDoor) = 'Sunday' then 1 else 0 end) --Taking into consideration the startdate being a sunday+(case when DATENAME(dw, CallCompleteDate) = 'Saturday' then 1 else 0 end) --Taking into consideration the startdate being a saturday+(select count(1) from tbl_holidays where tbl_holidays.TheDate between ConsentFormReceivedThroughDoor and CallCompleteDate) --check for holidays	
						) daysbetween
			from tbl_complaints
			where 
			(TheDivision = @Division) 
			and ((IssueType = 'Complaint') or (IssueType = 'MSP'))
			and (ConsentFormReceivedThroughDoor between @StartDate and @EndDate)
			and (DATEPART(WEEKDAY, ConsentFormReceivedThroughDoor)between 2 and 6)
			and
			(DATEDIFF(dd, ConsentFormReceivedThroughDoor, CallCompleteDate) + 1) -- Number of days between Start and End
					-
						(
							(DATEDIFF(wk, ConsentFormReceivedThroughDoor, CallCompleteDate)*2) --Weekend Days+(case when DATENAME(dw, ConsentFormReceivedThroughDoor) = 'Sunday' then 1 else 0 end) --Taking into consideration the startdate being a sunday+(case when DATENAME(dw, CallCompleteDate) = 'Saturday' then 1 else 0 end) --Taking into consideration the startdate being a saturday+(select count(1) from tbl_holidays where tbl_holidays.TheDate between ConsentFormReceivedThroughDoor and CallCompleteDate) --check for holidays between these two dates	
						) >= 0
			and
			(DATEDIFF(dd, ConsentFormReceivedThroughDoor, CallCompleteDate) + 1) -- Number of days between Start and End
			-
			(
				(DATEDIFF(wk, ConsentFormReceivedThroughDoor, CallCompleteDate)*2) --Weekend Days+(case when DATENAME(dw, ConsentFormReceivedThroughDoor) = 'Sunday' then 1 else 0 end) --Taking into consideration the startdate being a sunday+(case when DATENAME(dw, CallCompleteDate) = 'Saturday' then 1 else 0 end) --Taking into consideration the startdate being a saturday+(select count(1) from tbl_holidays where tbl_holidays.TheDate between ConsentFormReceivedThroughDoor and CallCompleteDate) --check for holidays	
			) <= @dayallowance	
)			
		RETURN
	end

GO

Can anyone help?

It parses okay but will not execute.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>