2017-09-15 56 views
0

如何在函数中传递一个标志?如何在函数中传递一个标志SQL

我有如下功能:

Alter function datedifferences (@leadid int,@businessid int,@flag int) 
Returns table 
as 
Return 
(  
    case when @flag=1 
     then 
      select case when datediff(MI,Lastactivedate,getdate()) < 1 
         then 'Online' 
         when cast(datediff(MI,Lastactivedate,getdate()) as varchar(10)) < 60 
         then cast(datediff(MI,Lastactivedate,getdate()) as varchar(10)) + 'Mins ago' 
         when cast(datediff(MI,Lastactivedate,getdate()) as varchar(10)) >= 60 
           and cast(datediff(MI,Lastactivedate,getdate()) as varchar(10)) <= 1440 
         then cast(datediff(MI,Lastactivedate,getdate())/60 as varchar(10)) + 'Hour ago' 
         when cast(datediff(MI,Lastactivedate,getdate()) as varchar(10)) > 1440 
         then cast(datediff(MI,Lastactivedate,getdate())/1440 as varchar(10)) + 'day ago' 
        end as lastactivedate 
       from userdetails ud 
       join enquiry eq on ud.contentid=eq.UserId 
       where [email protected] and [email protected] 
     else 
       select top 1 * from business where [email protected] 
     end as flag) 
+0

我使用的SQL Server 2012 –

+0

请花时间格式化您的问题。 – dotNET

+0

也完全不清楚你的目的究竟是什么。 – dotNET

回答

0

您可以使用 “联盟” 与Where条件 像

ALTER function datedifferences (@leadid int,@businessid int,@flag int) 
RETURNS TABLE 
AS RETURN 
( 
    select case when datediff(MI,Lastactivedate,getdate())<1 then 'Online' 
       when cast(datediff(MI,Lastactivedate,getdate()) as varchar(10))<60 then cast(datediff(MI,Lastactivedate,getdate()) as varchar(10))+ 'Mins ago' 
       when cast(datediff(MI,Lastactivedate,getdate()) as varchar(10))>=60 and cast(datediff(MI,Lastactivedate,getdate()) as varchar(10))<=1440 then cast(datediff(MI,Lastactivedate,getdate())/60 as varchar(10)) + 'Hour ago' 
       when cast(datediff(MI,Lastactivedate,getdate()) as varchar(10))>1440 then cast(datediff(MI,Lastactivedate,getdate())/1440 as varchar(10)) + 'day ago' 
      end as lastactivedate 
    from userdetails ud join enquiry eq on ud.contentid=eq.UserId 
    where [email protected] and [email protected] and @flag=1 
UNION ALL 
    SELECT top 1 lastactivedate from business where [email protected] and @flag<>1 
) 

ALTER function datedifferences (@leadid int,@businessid int,@flag int) 
RETURNS TABLE 
AS RETURN 
( 
SELECT 1 AS A,2 AS B,3 AS C WHERE @flag<>1 
UNION ALL 
SELECT 4 AS A,5 AS B,6 AS C WHERE @flag=1 
) 
相关问题