vendredi 1 juillet 2016

Difficulty in setting up query using 'CASE WHEN'

I have this query.

The logic of the select is whether the @USER_NAME parameter exists within the 3 following tables in their respective columns:

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

the test did not work as it should.

how best to assemble the query, it would be with the case or if?

How to assign the value correctly to @PROFILE_USER?

Help me pls

ERROR: Incorrect syntax near the keyword 'IN'.

Aucun commentaire:

Enregistrer un commentaire