SQL专门隐藏行

时间:2017-05-06 13:32:14

标签: sql sql-server

这是我的表结构和示例行:

Table and inserted rows

我有一个像这样的SQL查询:

WITH Test(ObjId, ObjectIcerik, KeyMi) AS
(
    SELECT 
        ObjId, ObjectIcerik, KeyMi 
    FROM 
        Tek
    WHERE 
        ObjId = 8

    UNION ALL

    SELECT 
        T.ObjId, T.ObjectIcerik, T.KeyMi 
    FROM 
        Tek T 
    INNER JOIN 
        Test as TE ON T.ObjParent = TE.ObjId
)
SELECT * 
FROM Test 

结果如下:

SQL query result

但如果父级KeyMi列为“1”,我还需要隐藏父子行。

我需要在SQL查询中更改哪些内容才能执行此操作?

1 个答案:

答案 0 :(得分:0)

您应该只能将条件添加到递归CTE:

WITH Test(ObjId, ObjectIcerik, KeyMi) AS (
    SELECT ObjId, ObjectIcerik, KeyMi FROM Tek
    WHERE ObjId = 8 AND (KeyMI <> 1 OR KeyMI IS NULL)
    UNION ALL
    SELECT T.ObjId, T.ObjectIcerik, T.KeyMi
    FROM Tek T INNER JOIN
         Test TE
         ON T.ObjParent = TE.ObjId
    WHERE (t.KeyMI <> 1 OR t.KeyMI IS NULL)
)   
SELECT *
FROM Test ;