我如何将其设为 UDF
Posted
技术标签:
【中文标题】我如何将其设为 UDF【英文标题】:How would I make this a UDF 【发布时间】:2014-08-26 15:06:14 【问题描述】:问题:如何从以下工作代码制作 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 行 'BEGIN' 附近的语法不正确。
【问题讨论】:
没用 - 怎么样?你收到错误了吗?如果是这样,请发布完整且准确的错误消息!如果没有 - 它以什么方式不起作用? 看起来您只是在“returns”之后缺少“TABLE”这个词 Msg 444,级别 16,状态 2,过程 RollingDateRAP,第 20 行,函数中包含的 Select 语句无法将数据返回到客户端。消息 455,级别 16,状态 2,过程 RollingDateRAP,第 20 行函数中包含的最后一条语句必须是返回语句。 @tab:看起来这样将错误减少到只有一个 Msg 102、Level 15、State 31、Procedure testudf、Line 81 'BEGIN'附近的语法错误。 是 [1]、[2] 等.. 占位符吗?如果假设它们代表数字,那么您应该将数值与日期进行比较。 【参考方案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
【讨论】:
感谢大家的帮助!我很感激以上是关于我如何将其设为 UDF的主要内容,如果未能解决你的问题,请参考以下文章
如何通过为用户提供将其设为默认应用程序的选项,在设备上将应用程序设为默认电话或助理处理程序