一、今天遇到这么一个功能,要从b,c表中读取一些数据,然后插入a中,但是a中的数据不能重复
解决方法:
INSERT INTO a
select * from
(
从b和c中读取数据
) as tempTable
where tempTable.唯一标示 not in (select 唯一标示 from a )
二、inner join 两个表,然后用得到的数据更新其中一个表
语法:
update tb1
set tb1.a = tb2.a
from tb1 inner join tb2
on tb1.id = tb2.id
我是这样理解的,先从from后面的语句读取一个表,然后按照set语法在表中修改数据
三、
多次插入同一个临时表
第一次用
select into #temp from
--后来用
insert into #temp select
四、 重A表中读取数据插入B表,在B表username会重复的情况下,保证A中的username不重复,排除重复的数据
insert into B select * from A as a where not exists(select 1 from B where username = a.username)