这个查询意味着什么......?

时间:2014-09-16 11:04:10

标签: sql database

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 uid=@uid where memberid=@memberid
--------------------------------------------------------------------------------
    update SAVINGDETAIL_2063_2064 set uid=@uid where memberid=@memberid
    update SAVINGDETAIL_2064_2065 set uid=@uid where memberid=@memberid
    update SAVINGDETAIL_2065_2066 set uid=@uid where memberid=@memberid
    update SAVINGDETAIL_2066_2067 set uid=@uid where memberid=@memberid
    update SAVINGDETAIL_2067_2068 set uid=@uid where memberid=@memberid
--------------------------------------------------------------------------------
    delete from #temp where memberid=@memberid
End

drop table #temp

2 个答案:

答案 0 :(得分:1)

此代码无效地更新了6个表中的uid以匹配uid表中的member s。它将要更新的表加载到临时表中,然后一次更新一个记录。

通常使用六个更新语句编写,每个表一个。第一个看起来像:

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

答案 1 :(得分:1)

查询基本上是为每个成员更新UIdSAVING_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 uid=@uid where memberid=@memberid  --> 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 uid=@uid where memberid=@memberid
    update SAVINGDETAIL_2064_2065 set uid=@uid where memberid=@memberid
    update SAVINGDETAIL_2065_2066 set uid=@uid where memberid=@memberid
    update SAVINGDETAIL_2066_2067 set uid=@uid where memberid=@memberid
    update SAVINGDETAIL_2067_2068 set uid=@uid where memberid=@memberid
--------------------------------------------------------------------------------
    delete from #temp where memberid=@memberid  --> Delete the record already updated from the temp table
End

drop table #temp