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

запрос в основном обновление UId из SAVING_DETAILS таблиц для каждого члена. Следуйте комментариям ниже;

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

Этот код довольно нерационально обновление uid с от 6 таблиц, чтобы соответствовать uid S в member таблице. Он загружает таблицу, которая будет обновляться во временную таблицу, а затем обновляет записи по одному.

Это, как правило, было бы написано с использованием шести операторов обновления, по одному для каждой таблицы. Первый может выглядеть следующим образом:

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