无法绑定多部分标识符 - 不是EXISTS

时间:2017-09-08 15:23:58

标签: sql sql-server tsql

在下面的代码中我收到此错误:

  

无法绑定多部分标识符tblExtraFeature.ProjectId

我认为这是一个范围问题,因为from tblExtraFeature在parens中。如果我从第一行删除tblExtraFeature.ProjectId,它就有效。是的ProjectId在tblExtraFeature中。我尝试为tblExtraFeature分配别名,但同样的问题。我怎样才能让它发挥作用?

select tblExtraFeature.ProjectId, tblFeature.FeatureId
  from tblFeature 
  with (NOLOCK)
  where  NOT EXISTS (select *
  from   tblExtraFeature 
    where tblFeature.FeatureId = tblExtraFeature.FeatureId)

编辑:
我应该在一开始就说明这一点......我想要得到的是tblExtraFeature中不在tblFeature中的所有行。

4 个答案:

答案 0 :(得分:1)

只需将tblExtraFeature和tblFeature表互相替换即可。并从select部分中删除tblFeature.FeatureId。

select *
from tblExtraFeature with (NOLOCK)
where NOT EXISTS (select *
from tblFeature  
where 
     tblFeature.FeatureId = tblExtraFeature.FeatureId )

答案 1 :(得分:0)

如果tblExtraFeature.FeatureId是引用tblFeature的外键,那么tblExtraFeature中不在tblFeature中的行应该是tblExtraFeature中featureId为null的行。

select * from tblExtraFeature where featureId is null

给出你想要的结果?

答案 2 :(得分:0)

尝试:

Select * from tblExtraFeature ef
left outer join tblFeature f on f.featureid = ef.featureid
where f.featureid IS NULL

答案 3 :(得分:0)

  

我想要得到的是tblExtraFeature中的所有行   不在tblFeature。

SELECT ProjectId, 
       FeatureId
  FROM tblExtraFeature 
  WITH (NOLOCK)
 WHERE FeatureId NOT IN (SELECT FeatureId
                           FROM tblFeature 
                        )