????--?????ж?(???????)???
????IF EXISTS(SELECT 1 FROM#FinalLotName)--???????ж???????????
????BEGIN
????--???SourceLotName??????????
????SELECT a.SN AS SourceLotName??a.VendorID??a.DateCode??a.SNNote??a.MaterialID AS ComMaterials INTO#SourceLotNameTable FROM CO_SN_GENERATION a WITH(NOLOCK)WHERE EXISTS(SELECT 1 FROM#FinalLotName b WHERE a.SN=b.SourceLotName)
????INSERT INTO#FinalCO_SN
????SELECT a.LotName??a.SourceLotName??d.SNCust??b.MaterialID??c.ComMaterials??b.MESOrderID??b.OnPlantID??c.VendorID??c.DateCode??c.SNNote FROM#FinalLotName a
????LEFT JOIN#WorkCO_SN1 b ON a.LotName=b.SN
????LEFT JOIN#SourceLotNameTable c ON a.SourceLotName=c.SourceLotName
????LEFT JOIN CO_SN_LINK_CUSTOMER d WITH(NOLOCK)ON a.LotName=d.SNMes
????END
????ELSE
????BEGIN
????--???SN???????????SourceSN??SNCust????------------------------------------------
????SELECT LotPK??LotName INTO#WorkLotPK410 FROM MMLots WITH(NOLOCK)WHERE EXISTS(SELECT 1 FROM#WorkCO_SN1 b WHERE b.SN=MMLots.LotID)
????SELECT a.LotPK??a.SourceLotPK into#WorkSourcePK420 FROM MMLotOperations a WITH(NOLOCK)WHERE EXISTS(SELECT 1 FROM#WorkLotPK410 b WHERE b.LotPK=a.LotPK)AND a.SourceLotPK IS NOT NULL
????SELECT a.LotPK??a.SourceLotPK??b.LotName INTO#WorkSourcePK430 FROM#WorkSourcePK420 a JOIN#WorkLotPK410 b ON a.LotPK=b.LotPK
????INSERT INTO#FinalLotName SELECT a.LotName??b.LotName AS SourceLotName??NULL FROM#WorkSourcePK430 a JOIN(SELECT LotPK??LotName FROM MMLots WITH(NOLOCK))b on a.SourceLotPK=b.LotPK--b??????????WHERE RowDeleted=0?????
????SELECT a.LotName??a.SourceLotName??b.SNCust INTO#FinalLotNameX440 FROM#FinalLotName a LEFT JOIN CO_SN_LINK_CUSTOMER b WITH(NOLOCK)ON a.LotName=b.SNMes
????DELETE FROM#FinalLotName
????INSERT INTO#FinalLotName SELECT LotName??SourceLotName??SNCust FROM#FinalLotNameX440
????-------------------------------------------------------------------------------------
????SELECT a.SN AS SourceLotName??a.VendorID??a.DateCode??a.SNNote??a.MaterialID AS ComMaterials INTO#SourceLotNameTable2 FROM CO_SN_GENERATION a WITH(NOLOCK)WHERE EXISTS(SELECT 1 FROM#FinalLotName b WHERE a.SN=b.SourceLotName)
????INSERT INTO#FinalCO_SN
????SELECT a.LotName??a.SourceLotName??a.SNCust??b.MaterialID??c.ComMaterials??b.MESOrderID??b.OnPlantID??c.VendorID??c.DateCode??c.SNNote FROM#FinalLotName a
????LEFT JOIN#WorkCO_SN1 b ON a.LotName=b.SN
????LEFT JOIN#SourceLotNameTable2 c ON a.SourceLotName=c.SourceLotName
????END
????END
????--2.2
????IF@DateCode<>''
????BEGIN
????SELECT Val INTO#WorkDateCode FROM fn_String_To_Table(@DateCode??'??'??1)
????--??@DataCode??????????????SourceSN
????SELECT SN AS SourceSN??MaterialID AS ComMaterials??VendorID??DateCode??SNNote INTO#WorkSourceSNT1 FROM CO_SN_GENERATION a WITH(NOLOCK)WHERE EXISTS(SELECT 1 FROM#WorkDateCode b WHERE a.DateCode=b.Val)
????----------------------------------------------------------------------------------------------------
????--?????ж?(???????)???
????IF EXISTS(SELECT 1 FROM#FinalCO_SN)--???????ж???????????
????BEGIN
????SELECT a.LotName??a.SourceLotName??a.SNCust??a.MaterialID??a.ComMaterials??a.MESOrderID??a.OnPlantID??a.VendorID??a.DateCode??a.SNNote INTO#TMP51 FROM#FinalCO_SN a WHERE EXISTS(SELECT 1 FROM#WorkDateCode b WHERE a.DateCode=b.Val)
????DELETE FROM#FinalCO_SN
????INSERT INTO#FinalCO_SN SELECT LotName??SourceLotName??SNCust??MaterialID??ComMaterials??MESOrderID??OnPlantID??VendorID??DateCode??SNNote FROM#TMP51
????END
????ELSE
????BEGIN
????IF EXISTS(SELECT 1 FROM#FinalLotName)
????BEGIN
????--???SourceLotName??????????
????SELECT a.SourceSN??a.VendorID??a.DateCode??a.SNNote??a.ComMaterials INTO#SourceLTX5 FROM#WorkSourceSNT1 a WHERE EXISTS(SELECT 1 FROM#FinalLotName b WHERE a.SourceSN=b.SourceLotName)
????--???SN??????????
????SELECT SN??MaterialID??MESOrderID??OnPlantID INTO#WorkSNT510 FROM CO_SN_GENERATION a WITH(NOLOCK)
????WHERE SNType='IntSN'AND SNRuleName='ProductSNRule'AND OnPlantID=@OnPlant
????AND EXISTS(SELECT 1 FROM#FinalLotName b WHERE a.SN=b.LotName)
????INSERT INTO#FinalCO_SN
????SELECT a.LotName??a.SourceLotName??d.SNCust??b.MaterialID??c.ComMaterials??b.MESOrderID??b.OnPlantID??c.VendorID??c.DateCode??c.SNNote FROM#FinalLotName a
????LEFT JOIN#WorkSNT510 b ON a.LotName=b.SN
????LEFT JOIN#WorkSourceSNT1 c ON a.SourceLotName=c.SourceSN
????LEFT JOIN CO_SN_LINK_CUSTOMER d WITH(NOLOCK)ON a.LotName=d.SNMes
????END
????ELSE
????BEGIN