为什么 SQL Server 会这样。我在 SQL 2005 上运行它。
IN 子句不验证子查询中的列名,而是根据外部查询中的表名对其进行验证。这是获取的示例
Create table #table1(col1 int, col2 char(10), col3 char(15));
Create table #table2(col10 int, col11 char(10), col2 char(15));
insert into #table1(col1, col2, col3)
select 1, 'one', 'three'
insert into #table1(col1, col2, col3)
select 2, 'two', 'three'
insert into #table1(col1, col2, col3)
select 3, 'three', 'four'
insert into #table2(col10, col11, col2)
select 1, 'one', 'three'
insert into #table2(col10, col11, col2)
select 2, 'two', 'three'
insert into #table2(col10, col11, col2)
select 3, 'three', 'four'
select * from #table1
where col1 IN
(select col1 from #table2)
好像我只是选择“从#table2中选择col1”并运行它会吐一个错误
Msg 207, Level 16, State 1, Line 1
Invalid column name 'col1'.