如果SQL数据库中A表和B表通过key关联,那么如何进行批量更新数据呢?下面就将为您介绍这种批量更新数据的SQL语句写法,供您参考,如果您在SQL批量更新数据方面遇到过问题,不妨一看,相信对您会有所启迪。
A,B表通过key关联的批量更新数据方法:
批量修改:{
update A
set A.qcye=B.qcye
from B
where A.kmdm=B.kmdm
and A.fmonth=B.fmonth
and A.fmonth=0
}
修改之后的查询,结果正确,OK
{
select * from A
where fmonth=0 and qcye<>0
and kmdm like '1606%' and len(kmdm)=10 order by kmdm
select * from B where fmonth=0 and qcye<>0
and kmdm like '1606%' and len(kmdm)=10 order by kmdm
}
------更新採購單身 是否免費,是否退貨 狀態------
drop table tempYY002
SELECT [MANDT]
,[EBELN]
,[EBELP]
,[MTART]
,[BUKRS]
,[WERKS]
,[REPOS]=case when [REPOS]='X' then '否' else '是' end
,[RETPO]=case when [RETPO]='X' then '是' else '否' end
into tempYY002
FROM [YY].[dbo].[採購單單身YY$]
UPDATE [11].[dbo].[採購單單身] SET [11].[dbo].[採購單單身].[是否免費]=b.[REPOS],[11].[dbo].[採購單單身].[是否退貨]=b.[RETPO]
FROM tempYY002 AS b
WHERE b.[EBELN]=[11].[dbo].[採購單單身].[採購單號] AND [11].[dbo].[採購單單身].[採購單項次]=b.[EBELP]
select * from [11].[dbo].[採購單單身]