Sql server 如何通过不使用多个插入来编写一个查询,在这些插入中,所有客户都将有另一个具有相同地址的地址3?

Sql server 如何通过不使用多个插入来编写一个查询,在这些插入中,所有客户都将有另一个具有相同地址的地址3?,sql-server,Sql Server,我在SQL SERVER中有一个如下所示的表客户 CUSTOMER(CUSTOMERID,ADDRESSID,ADDRESSTYPEID) values(1,1001,2);(2,1002,2);(3,1003,2);(4,1004,2) 如何通过不使用多个插入来编写一个查询,在这些插入中,所有客户都将有另一个具有相同地址的地址3 insert into customer_address (customerid, addressid, addresstypeid) select custome

我在SQL SERVER中有一个如下所示的表客户

CUSTOMER(CUSTOMERID,ADDRESSID,ADDRESSTYPEID)
values(1,1001,2);(2,1002,2);(3,1003,2);(4,1004,2)
如何通过不使用多个插入来编写一个查询,在这些插入中,所有客户都将有另一个具有相同地址的地址3

insert into customer_address (customerid, addressid, addresstypeid)
select customerid, addressid, 3
from customer_address
where addresstypeid = 2 /* where might not be needed */
要仅为那些尚未将
addresstypeid
设置为3的客户插入:

insert into customer_address (customerid, addressid, addresstypeid)
select customerid, addressid, 3
from customer_address c
where addresstypeid = 2
  and not exists (
    select 1
    from customer_address i
    where i.customerid = c.customerid 
      and i.addresstypeid = 3
      )