Monday, June 6, 2016

Function in SQL

USE [CASKOENIGDB]
GO
/****** Object:  UserDefinedFunction [dbo].[getFullMonthforRC]    Script Date: 06/08/2016 17:17:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER function [dbo].[getFullMonthforRC](@TrainerID int,@CourseSDate datetime,@CourseEDate datetime,@LocationId int,@weekendstatus varchar(30))
returns  @TableofDates table(TrainerID int not null,trngdate datetime not null,locationid int not null)
Begin
declare @trnid int
declare @lnid int
Declare @startDate datetime
declare @weekendbatch varchar(30)
set @trnid=@TrainerID
set @startDate=@CourseSDate
Declare @endDate datetime
set @endDate=@CourseEDate
set @lnid=@LocationId
set @weekendbatch=@weekendstatus
while @startDate<=@endDate
begin

if (@weekendbatch ='Weekend')  begin
IF (DATENAME(W,@startDate)='SATURDAY' or DATENAME(W,@startDate)= 'SUNDAY') BEGIN
INSERT INTO @TableOfDates(TrainerID,trngdate,locationid) VALUES (@trnid,@startDate,@lnid)
set @startDate=DATEADD(DAY, 1, @startDate)
END
ELSE BEGIN
set @startDate=DATEADD(DAY, 1, @startDate)

END
end
else IF CharIndex('5 Days',@weekendbatch)>0 begin
if @lnid= 5 begin
IF (DATENAME(W,@startDate)='THURSDAY' or DATENAME(W,@startDate)= 'FRIDAY') BEGIN
set @startDate=DATEADD(DAY, 1, @startDate)
END
ELSE BEGIN
INSERT INTO @TableOfDates(TrainerID,trngdate,locationid) VALUES (@trnid,@startDate,@lnid)
set @startDate=DATEADD(DAY, 1, @startDate)
END
end
else begin
IF (DATENAME(W,@startDate)='SUNDAY' or DATENAME(W,@startDate)= 'SATURDAY') BEGIN
set @startDate=DATEADD(DAY, 1, @startDate)
END
ELSE BEGIN
INSERT INTO @TableOfDates(TrainerID,trngdate,locationid) VALUES (@trnid,@startDate,@lnid)
set @startDate=DATEADD(DAY, 1, @startDate)
END
end
end
else IF CharIndex('7 Days',@weekendbatch)>0 begin
INSERT INTO @TableOfDates(TrainerID,trngdate,locationid) VALUES (@trnid,@startDate,@lnid)
set @startDate=DATEADD(DAY, 1, @startDate)
end
else IF CharIndex('Daily',@weekendbatch)>0 begin
INSERT INTO @TableOfDates(TrainerID,trngdate,locationid) VALUES (@trnid,@startDate,@lnid)
set @startDate=DATEADD(DAY, 1, @startDate)
end
else IF CharIndex('Weekly',@weekendbatch)>0 begin
INSERT INTO @TableOfDates(TrainerID,trngdate,locationid) VALUES (@trnid,@startDate,@lnid)
set @startDate=DATEADD(DAY, 1, @startDate)
end
else IF CharIndex('6 Days',@weekendbatch)>0 begin
if @lnid= 5 begin
IF  DATENAME(W,@startDate)= 'FRIDAY' BEGIN
set @startDate=DATEADD(DAY, 1, @startDate)
END
ELSE BEGIN
INSERT INTO @TableOfDates(TrainerID,trngdate,locationid) VALUES (@trnid,@startDate,@lnid)
set @startDate=DATEADD(DAY, 1, @startDate)
END
end
else begin
IF DATENAME(W,@startDate)='SUNDAY'  BEGIN
set @startDate=DATEADD(DAY, 1, @startDate)
END
ELSE BEGIN
INSERT INTO @TableOfDates(TrainerID,trngdate,locationid) VALUES (@trnid,@startDate,@lnid)
set @startDate=DATEADD(DAY, 1, @startDate)
END
end

end
else if @weekendbatch is null begin
INSERT INTO @TableOfDates(TrainerID,trngdate,locationid) VALUES (@trnid,@startDate,@lnid)
set @startDate=DATEADD(DAY, 1, @startDate)
end
end
return
End

No comments:

Post a Comment