日期:2014-05-18  浏览次数:20533 次

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

/*
肿么办啊,肿么办啊
*/
create proc proc_CopyMomByBomId
    @inbomid int, @inmodid int -- 传入一个BomId号和订单明细Id号
as
begin
    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 @bomid=@inbomid 
    delete from bom_Opcomponent where BomId=@bomid -- 删除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 MoDId=@inmodid
    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
/* 问题1
tmpOpComponent表有数据的时候,我要从bom_Opcomponent这张表删除原始BomId号
这个delete我不知道应该放在哪了
放在游标里面,新插入的数据又会删掉
 */
        delete from bom_Opcomponent where BomId=@bomidbak
        fetch next from cur_SelectOpSeq into @bomidbak,@define15bak
        while @@fetch_status=0
        begin
            -- 判断子件工序号是否等于默认工序号
            if @opseq=@define15bak
            begin
                -- 如果相等则将数据插入原始BomId号
                declare @p5 int
                declare @p6 int
                exec sp_GetID @RemoteId=N'00',@cAcc_Id='118',@cVouchType=N'bom_opcomponent',@iAmount=1,@iFatherId=@p5 output,@iChildId=@p6 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,@iFatherId=@p7 output,@iChildId=@p8 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_SelectCompByM