当前位置: 代码迷 >> 数据仓库 >> 触发器修改事件的有关问题
  详细解决方案

触发器修改事件的有关问题

热度:89   发布时间:2016-05-05 15:51:44.0
触发器修改事件的问题。
仓库盘点时如有损坏产品,出库业务里要同时增加一条报损出库信息;
如果有多出的产品,入库业务里要同时增加一条报溢入库信息;
需要用触发器来实现,如果损益数量大于0,就往入库业务中插入一条信息,如果损益数量小于0,就往出库业务中插入一条信息,
现在的问题是,多出的产品可以插入到入库业务中;但是报损的产品插入错误了,出库业务中不但插入了报损的数据,把其他损益数量为0的数据也插进去了;不知道哪位高手可以帮忙解决.错误触发器如下:
--盘点触发器
CREATE trigger ti_stock_盘点D on dbo.stock_盘点D for update as
begin
declare @单号 varchar(50)
declare @材料编号 varchar(50)
declare @材料名称 varchar(200)
declare @数量  decimal(18,2)
declare @规格  varchar(50)
declare @型号  varchar(50)
declare @单位  varchar(50)
declare @批次  varchar(50)
declare @采购价 decimal(18,2)
declare @销售价 decimal(18,2)

declare @日期 datetime
declare @分公司 varchar(50)
declare @仓库 varchar(50)
declare @经办人 varchar(50)
declare @经办部门 varchar(50)
declare @备注 varchar(500)


--新增仓库明细
declare num_cursor cursor for
Select 单号,材料编号,材料名称,损益数量,规格,型号,单位,批次 from stock_盘点D
open num_cursor
fetch num_cursor 
into @单号,@材料编号,@材料名称,@数量,@规格,@型号,@单位,@批次

While @@Fetch_status=0
begin

--盘点明细
--select  @单号=单号,@材料编号=材料编号,@材料名称=材料名称,@数量=损益数量,@规格=规格,@型号=型号,@单位=单位,@批次=批次 from stock_盘点D

--盘点主表
Select @日期=日期,@分公司=分公司,@仓库=仓库,@经办人=经办人,@经办部门=经办部门,@备注=备注 from stock_盘点H where [email protected]

--新增仓库主表
if @数量 < 0
begin
select * from stock_出入库H where [email protected]
if  @@RowCount <1 
       insert into stock_出入库H(单号,日期,分公司,方向,类型,仓库,经办人,经办部门,备注)values([email protected],@日期,@分公司,'出库','103',@仓库,@经办人,@经办部门,@备注)
else
   update stock_出入库H set [email protected],[email protected],[email protected],[email protected],[email protected],[email protected] where [email protected]

end
if @数量 > 0
begin
select * from stock_出入库H where [email protected]
if  @@RowCount <1 
   insert into stock_出入库H(单号,日期,分公司,方向,类型,仓库,经办人,经办部门,备注)values([email protected],@日期,@分公司,'入库','103',@仓库,@经办人,@经办部门,@备注)
else
   update stock_出入库H set [email protected],[email protected],[email protected],[email protected],[email protected],[email protected] where [email protected]

end


Select @采购价=采购价,@销售价=客户价 from stock_材料 where [email protected]

if @数量 < 0
begin
select * from stock_出入库D where [email protected] and [email protected]
if  @@RowCount <1 
   insert into stock_出入库D(单号,材料编号,材料名称,出库数量,规格,型号,单位,采购价,销售价,批次)values([email protected],@材料编号,@材料名称,@数量*(-1),@规格,@型号,@单位,@采购价,@销售价,@批次)
-- else
--    update stock_出入库D set [email protected]*(-1),[email protected],[email protected],[email protected],[email protected] where [email protected] and [email protected]

end
if @数量 > 0
begin
select * from stock_出入库D where [email protected] and [email protected]
if  @@RowCount <1 
   insert into stock_出入库D(单号,材料编号,材料名称,入库数量,规格,型号,单位,采购价,销售价,批次)values([email protected],@材料编号,@材料名称,@数量,@规格,@型号,@单位,@采购价,@销售价,@批次)
-- else
--    update stock_出入库D set [email protected],[email protected],[email protected],[email protected],[email protected] where [email protected] and [email protected]

end

Fetch num_cursor
Into  @单号,@材料编号,@材料名称,@数量,@规格,@型号,@单位,@批次

end 

Close num_cursor
Deallocate num_cursor

end
  相关解决方案