小编典典

根据情况选择

sql

我正在写一个存储过程。我在EP.Clerepartition专栏上有问题。我想根据情况选择此列。我需要在哪里设置代码?

我尝试了这段代码,但附近出现错误 EP.CleRepartition

“多部分标识符无法绑定”

我的查询正确无误还是可以用其他方式做到?

@PROJET_ID int = NULL,  @RoleID int=NULL

declare @query varchar (250) set @query= (select EP.CleRepartition
from EQUIPE_PROJET EP where EP.Projet_Id=@PROJET_ID AND EP.Role_Id=3
and EP.CleRepartition Is not null )

SELECT  U.USR_PRENOM ,U.USR_LOGIN,       EP.CleRepartition=     case
EP.CleRepartition 
      when @RoleID=1 and @requete IS NOT NULL then 100 - @requete 
      when EP.Role_Id=2 and @requete IS NOT NULL then 20
      ELSE NULL      END   
 FROM [EQUIPE_PROJET] EP
 INNER JOIN UTILISATEUR U ON  U.USR_ID = [User_Id]
 INNER JOIN Ref_Role_Eq RE ON RE.Role_Eq_Id = [Role_Id] 
 WHERE (@PROJET_ID IS NULL OR @PROJET_ID = [Projet_Id]) AND EP.Deletion_Date IS NULL
 AND (@RoleID is NULL or [Role_Id] =@RoleID)

我期望输出usr名称,usr登录名,角色名称和cleRepartition。


阅读 173

收藏
2021-04-15

共1个答案

小编典典

你能这样尝试吗?

SELECT  U.USR_PRENOM ,U.USR_LOGIN,      
CleRepartition=      
CASE
     when @RoleID=1 and @requete IS NOT NULL then 100 - @requete 
     when EP.Role_Id=2 and @requete IS NOT NULL then 20
     ELSE NULL      
END   
FROM [EQUIPE_PROJET] EP   
INNER JOIN UTILISATEUR U ON  U.USR_ID = [User_Id] -- Please define this user id belongs to which table   
INNER JOIN Ref_Role_Eq RE ON RE.Role_Eq_Id = [Role_Id]
2021-04-15