将SQL case语句转换为存储过程?

时间:2012-04-02 12:00:09

标签: sql function case

我有一个非常大的脚本,它创建了多个视图。在许多视图中,使用了相同的脚本。此脚本如下所示。

CASE WHEN pc.[user_name] IN
(
    SELECT [user_name]
    FROM people AS p WITH(NOLOCK) 
    WHERE p.status_id = 1 p.last_login > DATEADD(MONTH, -12, GETDATE())
    AND p.[user_name] NOT IN 
    (
        SELECT p.[user_name]
        FROM people AS p WITH(NOLOCK)
        WHERE p.status_id IN (1,2) AND p.[user_name] LIKE '%2'
    )
) THEN pc.[user_name]
ELSE 'standarduser' END AS created_by

有人能指出我在如何编写函数的正确方向,我可以传入pc.[user_name]并返回正确的值吗?我是SQL中的新功能。谢谢。

3 个答案:

答案 0 :(得分:2)

可以按如下方式创建该功能:

CREATE FUNCTION [dbo].[FunctionName] (@UserName VARCHAR(50)) 
RETURNS VARCHAR(50) 
AS
BEGIN   
    RETURN  COALESCE(
                (   SELECT  [User_name]
                    FROM    people AS p WITH(NOLOCK)      
                    WHERE   p.status_id = 1 
                    AND     p.last_login > DATEADD(MONTH, -12, GETDATE())     
                    AND     p.[user_name] NOT LIKE '%2'
                    AND     [User_name] = @UserName
                ), 'StandardUser')
END

我试图根据user_name是唯一的假设尽可能地简化你的选择,即user_name不能同时具有status_ID为1和2,这允许我从语句中删除NOT IN ,因为您首先选择status_ID为1的一组用户,然后从中删除status_ID为1或2且用户名以2结尾。因为第一组中没有人没有status_ID为1,你可以删除第一组中用户名以2结尾的那些,这不需要子查询,只需要一个where子句。

说了这么多,我仍然倾向于使用视图或类似的基于集合的解决方案来实现相同的结果。类似的东西:

SELECT  *, COALESCE(a.[User_Name], 'StandardUser') [NewUserName]
FROM    People p
        LEFT JOIN 
        (   SELECT  [User_name]
            FROM    people AS p WITH(NOLOCK)      
            WHERE   p.status_id = 1 
            AND     p.last_login > DATEADD(MONTH, -12, GETDATE())     
            AND     p.[user_name] NOT LIKE '%2'
        ) a
            ON p.[User_name] = a.[User_name]

答案 1 :(得分:0)

您可以尝试像这样的标量值函数

CREATE function [dbo].[MyFunction] (
@UserName nvarchar
) 
returns nvarchar
as
begin
CASE WHEN pc.[user_name] IN 
(     
    SELECT [user_name]     
    FROM people AS p WITH(NOLOCK)      
    WHERE p.status_id = 1 p.last_login > DATEADD(MONTH, -12, GETDATE())     

    AND p.[user_name] NOT IN 
    (         
        SELECT p.[user_name]         
        FROM people AS p WITH(NOLOCK)         
        WHERE p.status_id IN (1,2) AND p.[user_name] LIKE '%2'     
    )
)
THEN pc.[user_name] ELSE 'standarduser' END AS created_by 

end

GO

编辑: 哦,假设您正在使用

的SQL Server

答案 2 :(得分:0)

你可以把它变成一个视图或表函数(如果你需要其他逻辑或者想要传入一个参数)并将它包含在像表一样加入的其他视图/ proc中

;
with users as (
select p.user_name
  , p.status_id
  , p.last_login
from people as p with(nolock)
where p.status_id IN (1,2)
)
select DISTINCT u.user_name
  , CASE WHEN u.[user_name] IN 
     ( 
       SELECT [user_name] 
       FROM users AS u1 
       WHERE u1.status_id = 1 u1.last_login > DATEADD(MONTH, -12, GETDATE()) 
       AND u1.[user_name] NOT IN  
          ( 
             SELECT u2.[user_name] 
             FROM users AS u2  
             WHERE u2.status_id IN (1,2) AND u2.[user_name] LIKE '%2' 
          ) 
    ) THEN u.[user_name] 
      ELSE 'standarduser' END AS created_by 
from users as u