Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL中对主项和子项的依赖性_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

SQL中对主项和子项的依赖性

SQL中对主项和子项的依赖性,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我有一张这样的主桌 Product Status Product id A True 1 B True 2 C true 3 D True 4 E false 5 F True 6 现在我有了另一个表,它给出了产品的依赖关系 Product DependencyId C 2 C 5

我有一张这样的主桌

Product    Status  Product id
A          True     1
B          True     2
C          true     3
D          True     4
E          false    5
F          True     6
现在我有了另一个表,它给出了产品的依赖关系

Product    DependencyId
C             2
C             5
E             1
B             4
B             6
现在当我搜索C时,我可以看到两个产品依赖于C,现在这两个产品依赖于其他产品

假设我在搜索E,那么我发现这个产品只有一个依赖项

现在,我需要检查一个产品,如果任何依赖项是假的或不是。如果任何依赖项为false,那么我必须返回一些文本/值

结果


这应该列出状态为false的直接子级


这应该列出状态为false的直接子级


请使用您提供的示例数据给出您想要的示例。请使用您提供的示例数据给出您想要的示例。我在运行此脚本时遇到此错误。只有在使用列列表且启用identity_INSERT时,才能为表“tmp”中的identity列指定显式值。噢!我的主表也有一个标识列。现在我只使用必需的列,而不是顶部的select*。但是现在在这个步骤中,选择@cnt=count from tmp insert into tmp,我得到的*列名或提供的值的数量与表定义不匹配。我在运行此脚本时得到此错误。只有在使用列列表且启用identity_INSERT时,才能为表“tmp”中的标识列指定显式值。噢!我的主表也有一个标识列。现在,我不再在顶部选择*,而是只使用必需的列。但是,在这一步中,从tmp insert into tmp中选择@cnt=count,我得到的是*列名或提供的值的数量与表定义不匹配。
When product is C then output will be E (because the grand children of C (i.e. E) has false)

When product is B then output will be NULL (becuase none of the child of B or their sub childrens has false)
Declare @cnt int

Select * 
into #tmp
from mastera where Product_ID in (Select DependencyID from dbo.dependency where Product='C')

select @cnt=0
while @cnt<>(Select count(*) from #tmp)
begin 
    Select @cnt=count(*) from #tmp
    insert into #tmp
    Select m.* 
    from mastera m
    Left join  #tmp on m.Product=#tmp.Product 
    where m.Product_ID in (Select DependencyID from dbo.dependency where Product in (Select Product from #tmp where status=1))
    and #tmp.Product is null
end


Select * from #tmp  where Status=0

Drop table #tmp
SELECT  
    C.Product
FROM 
    MasterTable A
JOIN
    Dependency D ON D.Product = A.Product
JOIN
    MasterTable C ON C.ProductId = D.DependencyId AND C.Status = 0