pos_t_saleFlow是销售流水,pos_t_daySum是日汇总
主要是这两个表的问题
请帮忙看一下,谢谢
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
-- -------------------------------------------------------------------
-- 每增加一笔前台销售明细流水帐(pos_t_saleFlow)
-- 对应更新前台商品销售日结表(pos_t_daySum)
-- -------------------------------------------------------------------
ALTER  trigger pos_tr_i_saleflow on pos_t_saleflow for insert
as
beginif (select remote_flag from inserted) = '1' RETURNdeclare @ls_branchNO char(6), -- 仓库编码
@gs_branchNO char(6), -- 分店编码
@ls_ItemNO char(13), -- 商品编码
@ls_ClassNO char(6), -- 商品类别编码
@ln_saleQty decimal(14,4), -- 商品销售数量
@ln_sourcePrc decimal(14,4), -- 商品原来预定的单价
@ln_salePrc decimal(14,4), -- 商品销售单价
@ln_sourceAmt decimal(14,4), -- 商品原来预定的销售金额
@ln_saleAmt decimal(14,4), -- 商品销售金额
@ls_saleWay char(1), -- 销售方式
@ldt_saleDateTime datetime, -- 销售时间
@ls_operDate char(8), -- 字符型日期(当天),格式为:yyyymmdd
@ls_saleMan char(8), -- 营业员
-- 明细商品在商品销售日结表(pos_t_daySum)中是否已存在标志,'0'=不存在;'1'=已存在
@ls_ItemExistFlag char(1),
@ls_regionposition char(8), --区域编码
@ls_regionbranch char(8)   --分店编码
declare @ls_giveFlag char(1) -- 赠品标志
declare @ln_preQty decimal(14,4) -- 前次冲减数量(用于赠送统计)
declare @ln_preAmt decimal(14,4) -- 前次冲减金额(用于赠送统计)-- 当前销售流水数据
select @ls_branchNO=branch_no,
@ls_ItemNO=item_no,
@ln_saleQty=sale_qnty,
@ln_sourcePrc=source_price,
@ln_salePrc=sale_price,
@ls_saleWay=sell_way,
@ldt_saleDateTime=oper_date,
@ls_giveFlag = zs_flag,
@ls_saleMan = sale_man,
@ls_regionposition=region_position,
@ls_regionbranch=region_branch
from inserted
-- 计算原来预定的销售金额和实际的销售金额
select @ln_sourceAmt = @ln_saleQty * @ln_sourcePrc
select @ln_saleAmt = @ln_saleQty * @ln_salePrc
-- 转换字符型日期(当天),格式为:'112'=yyyymmdd
select @ls_operDate=convert(char(8),@ldt_saleDateTime,112)--分店代码
select @gs_branchNO = g_branch_no from branch_oneworld_relation where region_position=@ls_regionposition and region_branch=@ls_regionbranch-- 查找在商品销售日结表中是否已存在
if not exists(select 1 from pos_t_daysum
where(branch_no=@ls_branchNO and item_no=@ls_ItemNO and oper_date=@ls_operDate and region_position=@ls_regionposition and region_branch=@ls_regionbranch)) -- 不存在
select @ls_ItemExistFlag='0'
else
-- 已存在
select @ls_ItemExistFlag='1'if @ls_ItemExistFlag='0' -- 明细不存在,增加
if @ls_giveFlag = '0' -- 不是赠品
begin
if @ls_saleWay='A' -- 销售
begin
if @ln_saleQty > 0
insert into pos_t_daysum(branch_no,item_no,oper_date,ps_qnty,ps_amount,sale_price,pre_qnty,pre_amount,other1,other2,other3,region_position,region_branch) values
(@ls_branchNO,@ls_ItemNO,@ls_operDate,@ln_saleQty,@ln_saleAmt,@ln_salePrc,0,0,'1',@gs_branchNO,null,@ls_regionposition,@ls_regionbranch)
else -- 负销售计在退货中  
insert into pos_t_daysum(branch_no,item_no,oper_date,th_qnty,th_amount,sale_price,pre_qnty,pre_amount,other1,other2,other3,num1,num2,region_position,region_branch) values
(@ls_branchNO,@ls_ItemNO,@ls_operDate,-@ln_saleQty,-@ln_saleAmt,@ln_salePrc,0,0,'1',@gs_branchNO,null,@ln_saleQty,@ln_saleAmt,@ls_regionposition,@ls_regionbranch)
end
else if @ls_saleWay='B' -- 退货
insert into pos_t_daysum(branch_no,item_no,oper_date,th_qnty,th_amount,sale_price,pre_qnty,pre_amount,other1,other2,other3,region_position,region_branch) values
(@ls_branchNO,@ls_ItemNO,@ls_operDate,@ln_saleQty,@ln_saleAmt,@ln_salePrc,0,0,'1',@gs_branchNO,null,@ls_regionposition,@ls_regionbranch)
else if @ls_saleWay='D' -- 送货
insert into pos_t_daysum(branch_no,item_no,oper_date,sh_qnty,sh_amount,sale_price,pre_qnty,pre_amount,other1,other2,other3,region_position,region_branch) values
(@ls_branchNO,@ls_ItemNO,@ls_operDate,@ln_saleQty,@ln_saleAmt,@ln_salePrc,0,0,'1',@gs_branchNO,null,@ls_regionposition,@ls_regionbranch) end
else -- 是赠品
begin
insert into pos_t_daysum(branch_no,item_no,oper_date,zs_qnty,zs_amount,sale_price,pre_qnty,pre_amount,other1,other2,other3,region_position,region_branch) values
(@ls_branchNO,@ls_ItemNO,@ls_operDate,@ln_saleQty,@ln_saleAmt,@ln_salePrc,0,0,'1',@gs_branchNO,null,@ls_regionposition,@ls_regionbranch)
end
else -- 明细已存在,更新
if @ls_giveFlag = '0' -- 不是赠品
begin
if @ls_saleWay='A' -- 销售
begin
if @ln_saleQty > 0
update pos_t_daysum set ps_qnty=ps_qnty+@ln_saleQty, ps_amount=ps_amount+@ln_saleAmt, sale_price=@ln_salePrc, other1='1'
where branch_no=@ls_branchNO and item_no=@ls_ItemNO and oper_date=@ls_operDate and region_position=@ls_regionposition and region_branch=@ls_regionbranch
else -- 负销售计在退货中  
update pos_t_daysum set th_qnty=th_qnty-@ln_saleQty, th_amount=th_amount-@ln_saleAmt, sale_price=@ln_salePrc, other1='1', num1=num1+@ln_saleQty,num2=num2+@ln_saleAmt
where branch_no=@ls_branchNO and item_no=@ls_ItemNO and oper_date=@ls_operDate and region_position=@ls_regionposition and region_branch=@ls_regionbranch
end
else if @ls_saleWay='B' -- 退货
update pos_t_daysum set th_qnty=th_qnty+@ln_saleQty, th_amount=th_amount+@ln_saleAmt, sale_price=@ln_salePrc, other1='1'
where branch_no=@ls_branchNO and item_no=@ls_ItemNO and oper_date=@ls_operDate and region_position=@ls_regionposition and region_branch=@ls_regionbranch
else if @ls_saleWay='D' -- 送货
update pos_t_daysum set sh_qnty=sh_qnty+@ln_saleQty, sh_amount=sh_amount+@ln_saleAmt, sale_price=@ln_salePrc, other1='1'
where branch_no=@ls_branchNO and item_no=@ls_ItemNO and oper_date=@ls_operDate and region_position=@ls_regionposition and region_branch=@ls_regionbranch end
else -- 是赠品
begin
update pos_t_daysum set zs_qnty=zs_qnty+@ln_saleQty, zs_amount=zs_amount+@ln_saleAmt, sale_price=@ln_salePrc, other1='1'
where branch_no=@ls_branchNO and item_no=@ls_ItemNO and oper_date=@ls_operDate and region_position=@ls_regionposition and region_branch=@ls_regionbranch
endend
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

解决方案 »

  1.   

    我初步看了一下
    情况有两种该插入时没有插入
    或者 该更新新的没有更新
    如果你在查询分析器里同时更新2条记录以上估计就会发现出错
    在有每一条 insert update语句都应有用@@ERROR 判断成没成功 不成功应抛出异常 回滚
      

  2.   

    现在这个触发器只是针对insert而已。与更新无关
    插入记录的时候,都是一条一条的插入的,没有一次插入两条记录以上的
      

  3.   

    但是pos_t_daySum有没有约束什么的,或其他有可能出现插入失败的情况
    在这种情况一般是在应用程序中用事务处理