Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

sql - update table using cursor?

Update

the purpose of this excerise is to eliminate passing the @RegModifiedDateTime again what i want is i should be able to read ModifiedDateTime by passing Id

example: if i pass Id = 564 then i should be able to read

`schoold_Id and ModifiedDateTime`

end update

here is how my table looks like for SchoolRegistration:

school_id       id     active        modifydatetime
--------------------------------------------------
432        564       1               2008-12-14 13:15:38.750
342        564       1              2008-12-14 14:15:50.470
353        564       1              2008-12-14 14:19:46.703

end update

how do i loop to update my SchoolRegistration table? the id might have 1 or many rows in the SchoolRegistration but the thing is that RegModifiedDateTime is a unique for concurrency purpose and i should loop to get the right modifydatetime for that id.

alter procedure [dbo].[del_schoolRegistration]
    @Id bigint, 
    @RegModifiedDateTime datetime
as
begin  
    declare @rowsAffected int
    begin tran 


        --registration
        update SchoolRegistration
                   set Active = 0,
                    ModifiedDateTime = getdate()            
        where (Id = @Id and RegModifiedDateTime = @RegModifiedDateTime or @RegModifiedDateTime is null )


    if (@rowsAffected < 1) begin
        rollback tran
    end
    else begin
        commit tran
    end

    return @rowsAffected

end 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
   --registration
    ;with tmp as (
        select *, rn=ROW_NUMBER() over (partition by ID order by RegModifiedDateTime desc)
        from SchoolRegistration
        where (Id = @Id and RegModifiedDateTime = @RegModifiedDateTime or @RegModifiedDateTime is null ))
    update tmp
               set Active = 0,
                ModifiedDateTime = getdate()            
    WHERE rn=1

What happens here is that if you did not know the RegModifiedDateTime you are looking for (by passing @RegModifiedDateTime as NULL), the query will catch them all for the ID due to @RegModifiedDateTime is null, but update ONLY the LATEST RegModifiedDateTime based on the row_numbering and CTE table definition.

EDIT

The above query retains the option to pass in a direct @RegModifiedDateTime should a record other than the latest need updating. To always update only the latest, drop the WHERE filter against @RegModifiedDateTime completely

   --registration
    ;with tmp as (
        select *, rn=ROW_NUMBER() over (partition by ID order by RegModifiedDateTime desc)
        from SchoolRegistration
        where Id = @Id)
    update tmp
               set Active = 0,
                ModifiedDateTime = getdate()            
    WHERE rn=1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...