当前位置: 代码迷 >> Sql Server >> 【TravyLee】游标循环判断,数据插入重复的有关问题
  详细解决方案

【TravyLee】游标循环判断,数据插入重复的有关问题

热度:178   发布时间:2016-04-27 12:14:01.0
【TravyLee】游标循环判断,数据插入重复的问题
SQL code
/*肿么办啊,肿么办啊*/create proc proc_CopyMomByBomId    @inbomid int, @inmodid int -- 传入一个BomId号和订单明细Id号asbegin    declare @bomid int --母件id    declare @opcomponentid int -- 子件id    declare @sortseq int -- 行号    declare @opseq nchar(4) -- 工序行号    declare @componentid int -- 子件物料id    declare @effbegdate datetime -- 子件生效日期    declare @effenddate datetime -- 子件失效日期    declare @fvflag tinyint -- 变动批量    declare @baseqtyn float -- 基本用量-分子    declare @baseqtyd float -- 基本用量-分母    declare @compscrap float -- 子件损耗率    declare @auxunitcode nvarchar(35) -- 辅助计量单位    declare @changerate float -- 换算率    declare @auxbaseqtyn float -- 辅助基本用量    declare @producttype tinyint -- 产出类型(1:空/2:联产品/3:副产品)    declare @remark nvarchar(255) --备注    set @effbegdate='2000-01-01 00:00:00.000'    set @effenddate='2099-12-31 00:00:00.000'    set @[email protected]     delete from bom_Opcomponent where [email protected] -- 删除bom_Opcomponent表的所有BomId号数据    -- 游标:根据订单明细Id从mom_Moallocate表找子件,插入bom_Opcomponent    declare cur_SelectCompByMoDId cursor for        -- 查询那么多字段        select OpComponentId,SortSeq,OpSeq,ComponentId,FvFlag,BaseQtyN,BaseQtyD,CompScrap,AuxUnitCode,ChangeRate,AuxBaseQtyN,ProductType,Remark        from mom_Moallocate        where [email protected]    open cur_SelectCompByMoDId    fetch next from cur_SelectCompByMoDId into @opcomponentid,@sortseq,@opseq,@componentid,@fvflag,@baseqtyn,@baseqtyd,@compscrap,@auxunitcode,@changerate,@auxbaseqtyn,@producttype,@remark    while @@fetch_status=0    begin        declare @bomidbak int -- 原始BomId号        declare @define15bak int -- 默认工序号        -- 游标:取原始BomId号和默认工序        declare cur_SelectOpSeq cursor for            select bomid,define15 from tmpOpComponent        open cur_SelectOpSeq/* 问题1tmpOpComponent表有数据的时候,我要从bom_Opcomponent这张表删除原始BomId号这个delete我不知道应该放在哪了放在游标里面,新插入的数据又会删掉 */        delete from bom_Opcomponent where [email protected]        fetch next from cur_SelectOpSeq into @bomidbak,@define15bak        while @@fetch_status=0        begin            -- 判断子件工序号是否等于默认工序号            if @[email protected]            begin                -- 如果相等则将数据插入原始BomId号                declare @p5 int                declare @p6 int                exec sp_GetID @RemoteId=N'00',@cAcc_Id='118',@cVouchType=N'bom_opcomponent',@iAmount=1,@[email protected] output,@[email protected] output                insert into bom_OpComponent(BomId,OpComponentId,SortSeq,OpSeq,ComponentId,EffBegDate,EffEndDate,FvFlag,BaseQtyN,BaseQtyD,CompScrap,AuxUnitCode,ChangeRate,AuxBaseQtyN,ProductType,Remark)                values(@bomidbak,@p6,@sortseq,@define15bak,@componentid,@effbegdate,@effenddate,@fvflag,@baseqtyn,@baseqtyd,@compscrap,@auxunitcode,@changerate,@auxbaseqtyn,@producttype,@remark)            end            fetch next from cur_SelectOpSeq into @bomidbak,@define15bak        end        close cur_SelectOpSeq/*问题2下面的插入把所有数据都插进去了,我想除去工序号相等的,也就是上面那个判断的else放在上面那个else判断下,数据重复又插入N次*/        deallocate cur_SelectOpSeq        declare @p7 int        declare @p8 int        exec sp_GetID @RemoteId=N'00',@cAcc_Id='118',@cVouchType=N'bom_opcomponent',@iAmount=1,@[email protected] output,@[email protected] output        insert into bom_OpComponent(BomId,OpComponentId,SortSeq,OpSeq,ComponentId,EffBegDate,EffEndDate,FvFlag,BaseQtyN,BaseQtyD,CompScrap,AuxUnitCode,ChangeRate,AuxBaseQtyN,ProductType,Remark)        values(@bomid,@p8,@sortseq,@opseq,@componentid,@effbegdate,@effenddate,@fvflag,@baseqtyn,@baseqtyd,@compscrap,@auxunitcode,@changerate,@auxbaseqtyn,@producttype,@remark)        fetch next from cur_SelectCompByMoDId into @opcomponentid,@sortseq,@opseq,@componentid,@fvflag,@baseqtyn,@baseqtyd,@compscrap,@auxunitcode,@changerate,@auxbaseqtyn,@producttype,@remark    end    close cur_SelectCompByMoDId    deallocate cur_SelectCompByMoDIdend/*肿么办啊,肿么办啊*/


------解决方案--------------------