【问题标题】:How to add user-defined function too parameters?如何添加自定义函数的参数?
【发布时间】:2009-05-06 09:28:46
【问题描述】:

查询给了我一些错误:

消息 156,级别 15,状态 1,第 4 行 关键字“from”附近的语法不正确。 消息 156,第 15 级,状态 1,第 9 行 关键字“组”附近的语法不正确。

select 
Count(Page) as VisitingCount,
dbo.fn_GetActivityLogsArranger(CONVERT(VARCHAR(5),Date, 108)as [Time] 
 from scr_SecuristLog     
where Date between '2009-04-30' and '2009-05-02'    
and [user] in(select USERNAME               
    from scr_CustomerAuthorities 
where customerID=Convert(varchar,4) and ID=Convert(varchar,43) )    
group by CONVERT(VARCHAR(5),Date, 108) order by CONVERT(VARCHAR(5),Date, 108) asc 
------------------------------------------------------------------------------------------------------
create FUNCTION [dbo].[fn_GetActivityLogsArranger]
(
@time AS nvarchar(max)
)

RETURNS  nvarchar(max)
AS
BEGIN
declare @Return varchar(30)

select @Return = case 
when @time between '15:00' and '15:30' then '15:00-15:30'
when @time between '15:30' and '16:00' then '15:30-16:00'
when @time between '16:00' and '16:30' then '16:00-16:30'
when @time between '16:00' and '16:30' then '16:00-16:30' 
when @time between '16:30' and '17:00' then '16:30-17:00' 
when @time between '17:00' and '17:30' then '17:00-17:30' 
when @time between '17:30' and '18:00' then '17:30-18:00'
else 'Unknown'
end
 Return @Return
end
 

【问题讨论】:

    标签: sql sql-server


    【解决方案1】:

    你漏掉了一个括号

    select 
    Count(Page) as VisitingCount,
    dbo.fn_GetActivityLogsArranger(CONVERT(VARCHAR(5),Date, 108)**)** as [Time] 
     from scr_SecuristLog     
    where Date between '2009-04-30' and '2009-05-02'    
    and [user] in(select USERNAME               
        from scr_CustomerAuthorities 
    where customerID=Convert(varchar,4) and ID=Convert(varchar,43) )    
    group by CONVERT(VARCHAR(5),Date, 108) order by CONVERT(VARCHAR(5),Date, 108) asc 
    

    【讨论】:

    猜你喜欢
    • 2019-02-14
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    相关资源
    最近更新 更多