0

问题:如何从以下工作代码制作 UDF。

declare @currentweek as date
declare @1stweek as date
declare @2ndweek as date
declare @3rdweek as date
declare @4thweek as date
declare @5thweek as date

set @currentweek= dateadd(dd,0,(dateadd(DD,1-datepart(DW,GETDATE()),getdate())))
set @1stweek= dateadd(dd,7,(dateadd(DD,1-datepart(DW,GETDATE()),getdate())))
set @2ndweek= dateadd(dd,14,(dateadd(DD,1-datepart(DW,GETDATE()),getdate())))
set @3rdweek= dateadd(dd,21,(dateadd(DD,1-datepart(DW,GETDATE()),getdate())))
set @4thweek= dateadd(dd,28,(dateadd(DD,1-datepart(DW,GETDATE()),getdate())))
set @5thweek= dateadd(dd,35,(dateadd(DD,1-datepart(DW,GETDATE()),getdate())))

select


case 
    when ([1] >= @currentweek and [1]<@1stweek)and ([2] >= @currentweek and [2]<@1stweek) and ([3] >= @currentweek and [3]<@1stweek)then 'RAP'
    when ([2] >= @currentweek and [2]<@1stweek)and ([3] >= @currentweek and [3]<@1stweek) then 'RA'
    when ([1] >= @currentweek and [1]<@1stweek)and ([3] >= @currentweek and [3]<@1stweek) then 'AP'
    when [3] >= @currentweek and [3]<@1stweek then 'A'
    when ([1] >= @currentweek and [1]<@1stweek)and ([2] >= @currentweek and [2]<@1stweek) then 'RP'
    when [2] >= @currentweek and [2]<@1stweek then 'R' 
    when [1] >= @currentweek and [1]<@1stweek then 'P' 

    else null 
end as [current week]
,case 
    when ([1] >= @1stweek and [1]<@2ndweek)and ([2] >= @1stweek and [2]<@2ndweek) and ([3] >= @1stweek and [3]<@2ndweek)then 'RAP'
    when ([2] >= @1stweek and [2]<@2ndweek)and ([3] >= @1stweek and [3]<@2ndweek) then 'RA'
    when ([1] >= @1stweek and [1]<@2ndweek)and ([3] >= @1stweek and [3]<@2ndweek) then 'AP'
    when [3] >= @1stweek and [3]<@2ndweek then 'A'
    when (([1] >= @1stweek and [1]<@2ndweek)and ([2] >= @1stweek and [2]<@2ndweek)) then 'RP'
    when [2] >=@1stweek and [2] < @2ndweek then 'R'
    when [1] >=@1stweek and [1] < @2ndweek then 'P'
    else null
end as [Next week Week]

我试图研究CREATE Function命令:

我想出了

CREATE FUNCTION testudf()
returns table
as
begin

    Inserted the code

End
Go

然而这并没有奏效。有任何想法吗?

编辑:在退货旁边添加了单词表。

消息 102,级别 15,状态 31,过程 RollingDateRAP,第 81 行“开始
”附近的语法不正确。

4

1 回答 1

1

Ah try this:

CREATE FUNCTION testudf()
returns @tblOut  TABLE (
[current week] varchar(31)
,[Next week Week] varchar(31)
)
as
begin



   declare @currentweek as date
declare @1stweek as date
declare @2ndweek as date
declare @3rdweek as date
declare @4thweek as date
declare @5thweek as date

set @currentweek= dateadd(dd,0,(dateadd(DD,1-datepart(DW,GETDATE()),getdate())))
set @1stweek= dateadd(dd,7,(dateadd(DD,1-datepart(DW,GETDATE()),getdate())))
set @2ndweek= dateadd(dd,14,(dateadd(DD,1-datepart(DW,GETDATE()),getdate())))
set @3rdweek= dateadd(dd,21,(dateadd(DD,1-datepart(DW,GETDATE()),getdate())))
set @4thweek= dateadd(dd,28,(dateadd(DD,1-datepart(DW,GETDATE()),getdate())))
set @5thweek= dateadd(dd,35,(dateadd(DD,1-datepart(DW,GETDATE()),getdate())))

INSERT INTO @tblOut
select


case 
    when ([1] >= @currentweek and [1]<@1stweek)and ([2] >= @currentweek and [2]<@1stweek) and ([3] >= @currentweek and [3]<@1stweek)then 'RAP'
    when ([2] >= @currentweek and [2]<@1stweek)and ([3] >= @currentweek and [3]<@1stweek) then 'RA'
    when ([1] >= @currentweek and [1]<@1stweek)and ([3] >= @currentweek and [3]<@1stweek) then 'AP'
    when [3] >= @currentweek and [3]<@1stweek then 'A'
    when ([1] >= @currentweek and [1]<@1stweek)and ([2] >= @currentweek and [2]<@1stweek) then 'RP'
    when [2] >= @currentweek and [2]<@1stweek then 'R' 
    when [1] >= @currentweek and [1]<@1stweek then 'P' 

    else null 
end as [current week]
,case 
    when ([1] >= @1stweek and [1]<@2ndweek)and ([2] >= @1stweek and [2]<@2ndweek) and ([3] >= @1stweek and [3]<@2ndweek)then 'RAP'
    when ([2] >= @1stweek and [2]<@2ndweek)and ([3] >= @1stweek and [3]<@2ndweek) then 'RA'
    when ([1] >= @1stweek and [1]<@2ndweek)and ([3] >= @1stweek and [3]<@2ndweek) then 'AP'
    when [3] >= @1stweek and [3]<@2ndweek then 'A'
    when (([1] >= @1stweek and [1]<@2ndweek)and ([2] >= @1stweek and [2]<@2ndweek)) then 'RP'
    when [2] >=@1stweek and [2] < @2ndweek then 'R'
    when [1] >=@1stweek and [1] < @2ndweek then 'P'
    else null
end as [Next week Week];

RETURN;

End
Go
于 2014-08-26T15:16:06.127 回答