SELECT 
	t0.FID
	,t0e.FCPID
	into #tmp
FROM hw_mjcpdzb t0
	INNER JOIN hw_mjcpdzbs t0e on t0.FID = t0e.FID
GROUP BY
	t0.FID,t0e.FCPID
having
	count(1) =1
--SELECT * FROM #tmp
SELECT t0.FID
	,t0.FMJID
	,t0e.FEntryID
	,t0e.FCPID
	into #tmp1
FROM hw_mjcpdzb t0
	inner join hw_mjcpdzbs t0e on t0.FID = t0e.FID
WHERE exists (SELECT 1 from #tmp t WHERE t.fid = t0.fid AND t0e.FCPID = t0.FCPID)

/***********更新bom中的模具编码start***********/
--UPDATE t0 SET t0.FMJDZDZID = tt.FEntryID
SELECT 
	tt.FEntryID,t0.*
FROM
	#tmp1 tt
	inner join T_ENG_BOM t0 on t0.FMATERIALID = tt.FCPID AND t0.F_PAEZ_BASE = tt.FMJID

drop table #tmp
drop table #tmp1
/***********更新bom中的模具编码end***********/

--UPDATE t1e SET t1e.FMJDZDZID = t0.FMJDZDZID
SELECT 
	t0.FMJDZDZID,t1e.FMJDZDZID
FROM T_ENG_BOM t0
	INNER JOIN T_PRD_MOENTRY t1e on t0.FID = t1e.FBOMID
WHERE t0.FMJDZDZID != 0 AND t1e.FMJDZDZID != t0.FMJDZDZID

--UPDATE t0 SET t0.FMJDZDZID = t1.FMJDZDZID
SELECT t0.*
FROM T_PRD_PPBOM t0
	INNER JOIN T_ENG_BOM t1 on t0.FBOMID = t1.FID
WHERE t1.FMJDZDZID != 0 AND t1.FMJDZDZID != t0.FMJDZDZID
