使用'CASE WHEN'设置查询的难度

时间:2016-07-01 15:02:38

标签: sql if-statement case

我有这个问题。

select的逻辑是@USER_NAME参数是否存在于各自列中的3个以下表中:

MODEL_RESPONSIBLE MR> MR.USER_NAME
    RESPONSIBLE_PLANT_MODEL> RPM.USER_NAME_MPR
    RESPONSIBLE_AREA_MODEL> RAM.USER_NAME_AM

DECLARE 
@USER_NAME varchar(max) = 'lorem',
@PROFILE_USER varchar(max) = null;

SELECT  @USER_NAME, UserProfile =
      CASE @USER_NAME
         WHEN IN(RAM.USER_NAME_AM) THEN @PROFILE_USER = 'PROFILE 01'
         WHEN IN(RPM.USER_NAME_MPR) THEN @PROFILE_USER = 'PROFILE 02'
         WHEN IN(MR.USER_NAME) THEN @PROFILE_USER = MR.PROFILE_NAME
         ELSE @PROFILE_USER = ''
      END,
   UserProfile
FROM MODELO M 
INNER JOIN MODEL_RESPONSIBLE MR ON MR.ID_MODEL = M.ID_MODEL
INNER JOIN RESPONSIBLE_PLANT_MODEL RPM ON RPM.ID_MODEL = M.ID_MODEL
INNER JOIN RESPONSIBLE_AREA_MODEL RAM ON RAM.ID_MODEL = M.ID_MODEL

测试没有按预期工作。

如何最好地组装查询,它将与CASE或IF?

如何正确地将值分配给@PROFILE_USER?

  

错误:关键字“IN”附近的语法不正确。

4 个答案:

答案 0 :(得分:3)

您需要在语句的每一行中引用该字段

DECLARE 
@USER_NAME varchar(max) = 'lorem',
@PROFILE_USER varchar(max) = null;

SELECT  @USER_NAME,
      CASE 
         WHEN @USER_NAME IN(RAM.USER_NAME_AM) THEN @PROFILE_USER = 'PROFILE 01'
         WHEN @USER_NAME IN(RPM.USER_NAME_MPR) THEN @PROFILE_USER = 'PROFILE 02'
         WHEN @USER_NAME IN(MR.USER_NAME) THEN @PROFILE_USER = MR.PROFILE_NAME
         ELSE @PROFILE_USER
      END as UserProfile,
   UserProfile
FROM MODELO M 
INNER JOIN MODEL_RESPONSIBLE MR ON MR.ID_MODEL = M.ID_MODEL
INNER JOIN RESPONSIBLE_PLANT_MODEL RPM ON RPM.ID_MODEL = M.ID_MODEL
INNER JOIN RESPONSIBLE_AREA_MODEL RAM ON RAM.ID_MODEL = M.ID_MODEL

答案 1 :(得分:0)

CASE表达式可以写成:

SELECT  @USER_NAME,
      @PROFILE_USER =
      CASE @USER_NAME
         WHEN RAM.USER_NAME_AM THEN 'PROFILE 01'
         WHEN RPM.USER_NAME_MPR THEN  'PROFILE 02'
         WHEN MR.USER_NAME THEN  MR.PROFILE_NAME
         ELSE ''
      END as UserProfile,
   UserProfile
FROM MODELO M
...

答案 2 :(得分:0)

您的查询非常混乱,但我相信这与您希望CASE声明的内容一致:

@PROFILE_USER =
CASE
     WHEN @USER_NAME IN (RAM.USER_NAME_AM) THEN 'PROFILE 01'
     WHEN @USER_NAME IN (RPM.USER_NAME_MPR) THEN 'PROFILE 02'
     WHEN @USER_NAME IN (MR.USER_NAME) THEN MR.PROFILE_NAME
     ELSE ''
END

答案 3 :(得分:0)

您的查询不是SQL。它既有逻辑问题(内联接只有记录在所有三个表中才能成功)以及语法。

我相信这就是你想要做的事情:

SELECT @USER_NAME,
       (CASE WHEN EXISTS (SELECT 1 FROM MODEL_RESPONSIBLE MR WHERE MR.ID_MODEL = M.ID_MODEL AND RAM.USER_NAME_AM = @USER_NAME)
             THEN 'PROFILE 01'
             WHEN EXISTS (SELECT 1 FROM RESPONSIBLE_PLANT_MODEL RPM WHERE RPM.ID_MODEL = M.ID_MODEL AND RPM.USER_NAME_MPR = @USER_NAME)
             THEN 'PROFILE 02'
             ELSE (SELECT MR.PROFILE_NAME FROM RESPONSIBLE_AREA_MODEL RAM WHERE RAM.ID_MODEL = M.ID_MODEL AND MR.USER_NAME = @USER_NAME)
        END) as UserProfile
FROM MODELO M ;

如果你想设置变量@UserProfile,那么它将如下所示:

SELECT @UserProfile =
       (CASE WHEN EXISTS (SELECT 1 FROM MODEL_RESPONSIBLE MR WHERE MR.ID_MODEL = M.ID_MODEL AND RAM.USER_NAME_AM = @USER_NAME)
             THEN 'PROFILE 01'
             WHEN EXISTS (SELECT 1 FROM RESPONSIBLE_PLANT_MODEL RPM WHERE RPM.ID_MODEL = M.ID_MODEL AND RPM.USER_NAME_MPR = @USER_NAME)
             THEN 'PROFILE 02'
             ELSE (SELECT MR.PROFILE_NAME FROM RESPONSIBLE_AREA_MODEL RAM WHERE RAM.ID_MODEL = M.ID_MODEL AND MR.USER_NAME = @USER_NAME)
        END)
FROM MODELO M ;

或者,替代地,LEFT JOIN可能是合适的:

SELECT @USER_NAME, 
       (CASE WHEN RAM.USER_NAME_AM IS NOT NULL THEN 'PROFILE 01'
             WHEN RPM.USER_NAME_MPR THEN 'PROFILE 02'
             ELSE MR.PROFILE_NAME
        END) as UserProfile
FROM MODELO M LEFT JOIN
     MODEL_RESPONSIBLE MR
     ON MR.ID_MODEL = M.ID_MODEL AND RAM.USER_NAME_AM = @USER_NAME LEFT JOIN
     RESPONSIBLE_PLANT_MODEL RPM
     ON RPM.ID_MODEL = M.ID_MODEL AND RPM.USER_NAME_MPR = @USER_NAME LEFT JOIN
     RESPONSIBLE_AREA_MODEL RAM
     ON RAM.ID_MODEL = M.ID_MODEL AND MR.USER_NAME = @USER_NAME;

这是表达这种逻辑的更自然的方式。

请注意,您的语法建议使用SQL Server。您不能同时使用SQL Server中的单个SELECT语句设置变量和返回数据。