sql 批量操做數據

批量新增:mysql

insert into Table1(id,name,descr)
	select 1,'a','aa'
	union
	select 2,'b','bb'
	union
	select 3,'c','cc'
insert into Table1(id,name,descr)values
	(1,'a','aa'),
	(2,'b','bb'),
	(3,'c','cc')

批量修改sql

--ms_sql
update b  set  ClientName  =  a.name   
  from  a,b where  a.id  =  b.id
--oracle/db2
UPDATE A  SET (A1, A2, A3) = (SELECT B1, B2, B3 FROM B WHERE A.ID = B.ID)
--mysql
UPDATE A, B SET A1 = B1, A2 = B2, A3 = B3 WHERE A.ID = B.ID

表數據合併oracle

--ms_sql
merge into D_Student T --目標表
using (
                 select '001' as FId,'0000023' as FClassId,'張三' as FName,'2016-01-26' as FCreatDate,'59' as FStuNO
                                 union
                 select '002' as FId,'0000023' as FClassId,'李四' as FName,'2016-01-26' as FCreatDate,'76' as FStuNO
) S --源表
on T .FID = S . FID and T. FClassId = S. FClassId
when matched    --匹配即:當T表的FID 等於 S 的FID
then update set T.FName = S.FName , T . ClassId = S .ClassId
when not matched  --不匹配:當S表中有的 FID,但T 表中沒有
then insert values( S. FId , S. FClassId,S .FName , S .FCreatDate ,S. FStuNO)
when not matched by source and(T .FClassId= '0000023')   --不匹配:T表中有的FID,但S表中沒有
then delete ;
相關文章
相關標籤/搜索