内部案例到主要选择: - /

时间:2010-06-04 10:24:13

标签: sql

来自答案:

  ALTER PROCEDURE [dbo].[HardQ3] 
    (@ID_User nvarchar(15), @ID_ListGroupParIzm Integer,@CIzmer Integer, @CPoint Integer, @UserExt bit) 
     AS   

SELECT * FROM
(
    SELECT  
         C.ID_ListParIzm, 
         C.Hint, 
         CAST(CASE WHEN T2.ID_Param IS NULL AND T2.UserExt=@UserExt THEN 1 ELSE 0 END as bit) AS Visi ,
         T2.UserExt AS UserExt
    FROM 
         CfgListParIzm C  
         LEFT JOIN 
         (   SELECT 
             T.ID_Param,
             T.UserExt as UserExt 
          FROM
             TbUserParam T
          WHERE
             T.ID_User = @ID_User 
             AND T.ID_Izmerit = @CIzmer
             AND T.ID_Point = @CPoint 
         ) T2 On T2.ID_Param = C.ID_ListParIzm 
     WHERE 
        C.ID_ListGroupParIzm = @ID_ListGroupParIzm AND 
        C.VisibleOnTab=1
        --ORDER BY Num
) d
WHERE (UserExt IS NOT NULL)

T2.UserExt IS NOT NULL不会起作用,因为Where只在select之后工作,所以我需要在那里选择T2时实时使用它。

如何在主要选择中为每个节点使用此案例(T2.UserExt(带内部案例))

我真的希望这个问题是可以理解的:)

1 个答案:

答案 0 :(得分:1)

    PROCEDURE [dbo].[HardQ3] 
    (@ID_User nvarchar(15), @ID_ListGroupParIzm Integer,@CIzmer Integer, @CPoint Integer, @UserExt bit) 
     AS   

Select * from (

    SELECT  
         C.ID_ListParIzm, 
         C.Hint, 
         CAST(CASE WHEN T2.ID_Param IS NULL AND T2.UserExt=@UserExt THEN 1 ELSE 0 END as bit) AS Visi 
    FROM 
         CfgListParIzm C  
         LEFT JOIN 
         (   SELECT 
             T.ID_Param,
             T.UserExt as UserExt 
          FROM
             TbUserParam T
          WHERE
             T.ID_User = @ID_User 
             AND T.ID_Izmerit = @CIzmer
             AND T.ID_Point = @CPoint 
         ) T2 On T2.ID_Param = C.ID_ListParIzm 
     WHERE 
        C.ID_ListGroupParIzm = @ID_ListGroupParIzm AND 
        C.VisibleOnTab=1
        ORDER BY Num


)d where  UserExt IS NOT NULL