2014-09-16 3 views
-1
declare @memberid int 
declare @uid int 

select memberid, uid into #temp from member 

While (Select Count(*) From #Temp) > 0 
Begin 
    select top 1 @memberid= memberid, @uid=uid from #temp 
    update savingdetail set [email protected] where [email protected] 
-------------------------------------------------------------------------------- 
    update SAVINGDETAIL_2063_2064 set [email protected] where [email protected] 
    update SAVINGDETAIL_2064_2065 set [email protected] where [email protected] 
    update SAVINGDETAIL_2065_2066 set [email protected] where [email protected] 
    update SAVINGDETAIL_2066_2067 set [email protected] where [email protected] 
    update SAVINGDETAIL_2067_2068 set [email protected] where [email protected] 
-------------------------------------------------------------------------------- 
    delete from #temp where [email protected] 
End 

drop table #temp 
+3

이해할 수없는 부분이 있습니까? –

답변

1

: 같은 첫 번째는 볼 수 있습니다. 아래의 의견을 따르십시오.

select memberid, uid into #temp from member --> populate a temporary table with members details 

While (Select Count(*) From #Temp) > 0 --> Loop for each record in the temp table 
Begin 
    select top 1 @memberid= memberid, @uid=uid from #temp --> Select the top most record from the temp table 
    update savingdetail set [email protected] where [email protected] --> Update the UId of the savingdetail table with the one of the temp table 
-------------------------------------------------------------------------------- 

    ----> Updating the UId of the savingdetail tables with the one of the temp table 
    update SAVINGDETAIL_2063_2064 set [email protected] where [email protected] 
    update SAVINGDETAIL_2064_2065 set [email protected] where [email protected] 
    update SAVINGDETAIL_2065_2066 set [email protected] where [email protected] 
    update SAVINGDETAIL_2066_2067 set [email protected] where [email protected] 
    update SAVINGDETAIL_2067_2068 set [email protected] where [email protected] 
-------------------------------------------------------------------------------- 
    delete from #temp where [email protected] --> Delete the record already updated from the temp table 
End 

drop table #temp 
1

이 코드는 member 테이블의 uid과 일치하도록 6 테이블에서 uid을 비효율적으로 비효율적으로 업데이트합니다. 임시 테이블에 업데이트 할 테이블을로드 한 다음 한 번에 하나씩 레코드를 업데이트합니다.

더 일반적으로 각 테이블에 대해 하나씩 6 개의 update 문을 사용하여 작성됩니다. 쿼리는 기본적으로 각 구성원에 대한 UIdSAVING_DETAILS의 테이블을 업데이트

update sv 
    set sv.uid = m.uid 
    from member m join 
     savingdetail sv 
     on m.memberid = sv.memberid;