2011年7月12日星期二

删除重复数据的存储过程

CREATE PROCEDURE deldup

AS
    declare @bid integer,@bbid integer,@bname varchar(200)
    declare name_cursor cursor for  SELECT distinct a.bname FROM BuyerTable a INNER JOIN BuyerTable b ON a.Bname = b.Bname AND a.BuyerID <> b.BuyerID
   
    open name_cursor

    FETCH   NEXT   FROM   name_cursor  INTO   @bname

    WHILE   @@FETCH_STATUS   =   0
    BEGIN
        select @bid=min(buyerid) from buyertable where bname=@bname
        declare id_cursor cursor for select buyerid from buyertable where bname=@bname
        open id_cursor
        fetch next from id_cursor into @bbid
        while @@fetch_status=0
        begin
            update ProductionSell set buyerid=@bid where buyerid=@bbid
            fetch next from id_cursor into @bbid
        end
        close id_cursor
        deallocate id_cursor

        update buyertable set buyerid=@bid where bname=@bname
       
        FETCH   NEXT   FROM   name_cursor  INTO   @bname
    end
    close name_cursor
    deallocate name_cursor
   
GO

没有评论:

发表评论