尝试删除某些记录时,至少在SQL Server上遇到了奇怪的行为(我仍然需要检查其他SQL引擎)。>
我在SQL Server实例上测试了以下内容:
这里是一个SQL代码段。这是我正在尝试做的简化版本,可以肯定地讨论为什么要用这种方式进行查询,但是我的观点是不同的-为什么会发生。
drop table if exists #A drop table if exists #B create table #B (id char, foo char) -- I use different types for id columns just for the sake of readability create table #A (id int, b_id char) -- b_id is a link to #A, _not_ specified as FK insert into #B values('x', 'l') insert into #B values('y', 'm') insert into #A values(0, 'x') insert into #A values(1, 'z') insert into #A values(2, 'z') insert into #A values(3, 'y') insert into #A values(4, 'y') insert into #A values(5, 'y') -- there are 2 parent records in #B and 3 child records for each of them in #A select * from #A -- just to check, all good the data is there, all as expected, no problem -- now the fun part --(X) the following query, rightfully gives an error, as column b_id does not exist in #B -- select b_id from #B where foo='l' --(Y) the following query gives empty result, whereas I would expect an error: select * from #A where b_id in (select b_id from #B where foo='l') -- I can imagine that this has something to do with the fact that b_id exists in table #A in outer query --(Z) the following query deletes(!) all data in table #A: delete from #A where b_id in (select b_id from #B where foo='l') select * from #A -- once again, I can imagine that there is no error message "Invalid column name 'b_id'." because b_id exists in table #A in outer query
所以这是我的问题:
我在尝试删除某些记录时,至少在SQL Server上遇到了奇怪的行为(我仍然需要检查其他SQL引擎)。我在SQL Server实例上测试了以下内容:Microsoft SQL ...
查询