Clause where avec case

Bonjour,

J’ai un probleme de clause WHERE avec CASE :

FROM Table_Inter INNER JOIN Table_Reclam 
ON Table_Inter.INDICE = Table_Reclam.INDICE
WHERE
Retour_Presta = 'oui' AND (CAST(Table_Inter.DATE_REA AS DATETIME) >= @Date_Debut) AND (CAST(Table_Inter.DATE_REA AS DATETIME) <= @Date_Fin) AND
CASE (SELECT lblcontrat FROM Table_Inter where NumFiche=@num_fiche )
WHEN 'Robinetterie' THEN
	(ABS(DATEDIFF(day,(CAST(Table_Inter.DATEPREVU AS DATETIME)), (CAST(Table_Inter.DATE_REA AS DATETIME)))) <= 2) -- 48H
ELSE
	(ABS(DATEDIFF(day,(CAST(Table_Inter.DATE_APL AS DATETIME)), (CAST(Table_Inter.DATE_REA AS DATETIME)))) <= 1) -- 24H

Mais il me mets comme erreur
Msg 170, Niveau 15, État 1, Procédure GET_LISTE_INTERVENTION_DANS_DELAI, Ligne 159
Ligne 159 : syntaxe incorrecte vers ‘<’.

C’est la premiere clause Then du CASE…

Quelqu’un peut m’aider SVP…

Merci

C’est du mysql? oracle? pgsql? mssql?

Et si tu essayes < 3 et < 2 respectivement?

c’est du Sql Server pardon…

J’ai reussi à faire autrement , avec des if…
mais le probleme est que je me retrouve avec une ligne par select ! bref, pas propre et pas exploitable ! il faut que je puis concatener ma clause where en fonction du lblcontrat

Merci pour vos réponses

En fait, ton erreur doit être ça :

CASE (SELECT lblcontrat FROM Table_Inter where NumFiche=@num_fiche )
WHEN 'Robinetterie' THEN
(ABS(DATEDIFF(day,(CAST(Table_Inter.DATEPREVU AS DATETIME)), (CAST(Table_Inter.DATE_REA AS DATETIME)))) <= 2) -- 48H
ELSE
(ABS(DATEDIFF(day,(CAST(Table_Inter.DATE_APL AS DATETIME)), (CAST(Table_Inter.DATE_REA AS DATETIME)))) <= 1) -- 24H

Ce n’est même pas une bonne sous requête; Tu devrais plutôt avoir :

exists (
  select 1 from Table_Inter where NumFiche=@num_fiche and CASE lblcontrat WHEN 'Robinetterie' THEN
(ABS(DATEDIFF(day,(CAST(Table_Inter.DATEPREVU AS DATETIME)), (CAST(Table_Inter.DATE_REA AS DATETIME)))) <= 2) -- 48H
ELSE
(ABS(DATEDIFF(day,(CAST(Table_Inter.DATE_APL AS DATETIME)), (CAST(Table_Inter.DATE_REA AS DATETIME)))) <= 1)
)

Un case n’est pas fait pour s’utiliser dans un Where clause comme ca.
Un case ne retourne qu’un resultat, c’est comme si tu ecris:

Select top 10 * 
From truc 
Where 'abc'

A la limite tu pourrais faire

Select top 10 *
From truc
Where ID = Case (Select category From truc) 
  When 'xx' Then 1
  When 'xy' Then 2
  When 'yy' Then 3
End

Dans ton cas le plus facil c’est de faire une petite procedure avec plusieurs query pour rester simple et lisible.