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

しかし、それはうまくいきませんでした。何か案は?

編集: return の横に単語 table を追加しました。

メッセージ 102、レベル 15、状態 31、プロシージャ RollingDateRAP、行 81
'BEGIN' 付近の構文が正しくありません。

4

1 に答える 1