I’m trying to insert a table into another table,
insert into list2([PHONE])
select [column 0] + [column 1] as PHONE
from [list1]
where not exists (select * from list2
where list2.phone = [list1].phone)
but I’m getting an error:
Msg 207, Level 16, State 3, Line 1
Invalid column name 'phone'.
On list2, there are 2 columns, phone and area code. phone has the full phone number and area code has the area code.
On list1, there are also 2 columns, column 0 which has the area code and column 1 which is the first 6 digits of the phone number.
What am I doing wrong?
You’re not allowed to reference column aliases you construct in the
SELECTlist from theWHEREclause:Alternatively, you could place the construction of the
phonecolumn into a subquery within theFROMclause: