我如何将其作为UDF

时间:2014-08-26 15:06:14

标签: sql sql-server tsql user-defined-functions create-function

问题:如何使用以下工作代码制作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

然而,这不起作用。有什么想法吗?

编辑:在返回旁边添加了单词表。

  

Msg 102,Level 15,State 31,Procedure RollingDateRAP,第81行   'BEGIN'附近的语法不正确。

1 个答案:

答案 0 :(得分:1)

啊试试这个:

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