【问题标题】:SQL error when using select into where not exists query使用 select into where not exists 查询时出现 SQL 错误
【发布时间】:2012-09-29 22:04:20
【问题描述】:

我正在尝试将一个表插入另一个表,

insert into list2([PHONE])
select [column 0] + [column 1] as PHONE
from [list1]
where not exists (select * from list2
where list2.phone = [list1].phone)

但我收到一个错误:

Msg 207, Level 16, State 3, Line 1 Invalid column name 'phone'.

list2 上有 2 列,phonearea codephone 有完整的电话号码,area code 有区号。

list1 上也有 2 列,column 0 有区号,column 1 是电话号码的前 6 位。

我做错了什么?

【问题讨论】:

标签: sql-server select insert where


【解决方案1】:

您不能从WHERE 子句引用您在SELECT 列表中构造的列别名:

insert into list2([PHONE])
select [column 0] + [column 1] as PHONE
from [list1]
where not exists (select * from list2
where list2.phone = [list1].[column 0] + [list1].[column 1])

或者,您可以将phone 列的构造放入FROM 子句中的子查询中:

insert into list2([PHONE])
select PHONE
from (select [column 0] + [column 1] as PHONE from [list1]) l1
where not exists (select * from list2
where list2.phone = l1.phone)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多