code1: update ta set ta.iden = tm.iden from advancement_bonus as ta join ml as tm on ta.sa_ss = tm.sa_ss code2: update t1 set storename=off_name from tbl_customer_cancellation_d1 as t1 join tbl_sdk_mos as t2 on t1.storecode=t2.code where storename is null -- CODE3: -- 將沒有明細資料的主檔資料,貼上旗標 -- init USE SDKDW update tbl_r0712_iden set FWorkFlag=null update t1 set FWorkFlag='N' from tbl_r0712_iden as t1 LEFT join TBL_SDK_VOLUME as t2 on t1.IDEN=t2.CUST_IDEN where T2.PERIOD_END IS NULL -- 將沒有明細資料的主檔資料刪除 DELETE FROM TBL_R0712_IDEN WHERE IDEN IN ( SELECT T1.IDEN from tbl_r0712_iden as t1 LEFT join TBL_SDK_VOLUME as t2 on t1.IDEN=t2.CUST_IDEN where T2.PERIOD_END IS NULL ) -- 將沒有主檔的明細資料的刪除 DELETE FROM TBL_R0712_IdenMonth WHERE IDEN IN ( SELECT T1.IDEN from TBL_R0712_IdenMonth as t1 LEFT join TBL_R0712_Iden as t2 on t1.IDEN=t2.IDEN where T2.IDEN IS NULL )