我正在设置此存储过程,但出现此错误:
无法绑定多部分标识符
在选择的所有列上。
这是我的查询:
ALTER PROCEDURE [dbo].[Ps_GetEquipeProjet]
@PROJET_ID INT = NULL,
@RoleID INT = NULL
AS
BEGIN
DECLARE @requete VARCHAR(250)
SET @requete = (SELECT CleRepartition FROM EQUIPE_PROJET
WHERE Projet_Id = @PROJET_ID AND Role_Id = 3
AND CleRepartition IS NOT NULL)
SELECT
EP.Equipe_Projet_Id AS PROJET_ID,
U.USR_ID, U.USR_NOM, U.USR_PRENOM,
U.USR_LOGIN,
RE.Role_Eq_Libelle AS ROLE_NOM,
EP.CleRepartition = CASE
WHEN @RoleID = 1 AND @requete IS NOT NULL AND @requete = 75
THEN 25
WHEN @RoleID = 1 AND @requete IS NOT NULL AND @requete = 50
THEN 50
WHEN @RoleID = 2 AND @requete IS NOT NULL
THEN 20
ELSE NULL
END
FROM
[EQUIPE_PROJET] EP
INNER JOIN
UTILISATEUR U ON U.USR_ID = EP.[User_Id]
INNER JOIN
Ref_Role_Eq RE ON RE.Role_Eq_Id = EP.[Role_Id]
WHERE
(@PROJET_ID IS NULL OR @PROJET_ID = EP.[Projet_Id])
AND EP.Deletion_Date IS NULL
AND (@RoleID IS NULL OR [Role_Id] = @RoleID)
END
答案 0 :(得分:1)
就像我在评论中说的那样,在将带有UPDATE
的数据集返回到表示层的同一语句中,您无法SELECT
的列。您首先需要UPDATE
的值CleRepartition
,然后执行SELECT
。我无法对此进行测试,但是大概是您需要的:
ALTER PROCEDURE [dbo].[Ps_GetEquipeProjet] @PROJET_ID int = NULL,
@RoleID int = NULL
AS
BEGIN
DECLARE @requete varchar(250);
SET @requete = (SELECT CleRepartition
FROM EQUIPE_PROJET
WHERE Projet_Id = @PROJET_ID
AND Role_Id = 3
AND CleRepartition IS NOT NULL);
UPDATE EP
SET CleRepartition = CASE
WHEN @RoleID = 1
AND @requete IS NOT NULL
AND @requete = 75 THEN 25
WHEN @RoleID = 1
AND @requete IS NOT NULL
AND @requete = 50 THEN 50
WHEN @RoleID = 2
AND @requete IS NOT NULL THEN 20
ELSE NULL
END
FROM [EQUIPE_PROJET] EP
INNER JOIN UTILISATEUR U ON U.USR_ID = EP.[User_Id]
INNER JOIN Ref_Role_Eq RE ON RE.Role_Eq_Id = EP.[Role_Id]
WHERE (@PROJET_ID IS NULL
OR @PROJET_ID = EP.[Projet_Id])
AND EP.Deletion_Date IS NULL
AND (@RoleID IS NULL
OR [Role_Id] = @RoleID);
SELECT EP.Equipe_Projet_Id AS PROJET_ID,
U.USR_ID,
U.USR_NOM,
U.USR_PRENOM,
U.USR_LOGIN,
RE.Role_Eq_Libelle AS ROLE_NOM,
EP.CleRepartition
FROM [EQUIPE_PROJET] EP
INNER JOIN UTILISATEUR U ON U.USR_ID = EP.[User_Id]
INNER JOIN Ref_Role_Eq RE ON RE.Role_Eq_Id = EP.[Role_Id]
WHERE (@PROJET_ID IS NULL
OR @PROJET_ID = EP.[Projet_Id])
AND EP.Deletion_Date IS NULL
AND (@RoleID IS NULL
OR [Role_Id] = @RoleID);
END;
GO
换句话说,这里的内容称为“全部查询”,或者(如Aaron Bertrand所说的)是“厨房水槽”查询。确实,您想使用动态SQL进行此类查询,或者使用OPTION (RECOMPILE)
(我个人喜欢前者)。这是一些有关盖尔·肖(Gail Shaw)和贝特朗(Bertrand)的主题的文章:Catch-All Queries,Revisiting Catch-All Queries和#BackToBasics: An Updated "Kitchen Sink" Example