ALTER TRIGGER [Tu_Csell] ON dbo.B_Csell<br>--with encryption<br> FOR UPDATE <br>AS<br>--//销售发货单<br>begin<br> declare<br> @numrows int,<br> @numnull int,<br> @errno int,<br> @errmsg varchar(255),<br> @costtype char(10),<br> @updatecnt int<br> <br> set @updatecnt=@numrows<br><br> --记录为空返回-- <br> select @numrows = @@rowcount<br> if @numrows = 0<br> return<br> <br> --结案--<br> if update(F_stateID)<br> begin<br> set @numrows=(select count(*) from inserted<br> where isnull(F_stateID,'0')='6')<br> if @numrows>0<br> begin<br> if update(F_CsellID) or update(F_Csell) or update(F_note) or update(F_lock)<br> or update(F_create) or update(F_crdate) or update(F_firm)<br> or update(F_fdate) or update(F_Audit) or update(F_ADate) <br> begin<br> raiserror 130100 '不可以同时修改结案和其他内容!'<br> rollback transaction <br> return<br> end<br> end<br> end<br> <br><br> ----------------------发货单(加上下段语句时有出错提示)----------------------- <br> if update(F_stateID)<br> and exists (select 1 from deleted --非反结案<br> where isnull(F_stateID,'0')<>'6')<br> and exists (select 1 from inserted --非结案<br> where isnull(F_stateID,'0')<>'6')<br> begin<br> --审核--<br> if exists (select 1 from inserted<br> where isnull(F_stateID,'0')='3')<br> begin<br> --审核日期--<br> if exists (select 1 from inserted<br> where isnull(F_ADate,'')=''<br> and isnull(F_stateID,'0')='3')<br> begin <br> raiserror 130100 '审核日期不可以为空值!'<br> rollback transaction<br> return<br> end<br><br> --计算库存-----------<br> --添加表中没有的商品记录<br> insert into pub_stock (F_shopID,F_goodsID,F_goods,F_goitem,F_gostyle,F_unit)<br> select DISTINCT i1.F_wareID,t1.F_goodsID,t1.F_goods,t1.F_goitem,t1.F_gostyle,t1.F_unit<br> from B_Csellsub t1,inserted i1<br> where t1.F_CsellID=i1.F_CsellID<br> and not exists (select 1 from pub_stock c<br> where i1.F_wareID=c.F_shopID<br> and t1.F_goodsID=c.F_goodsID)<br><br> --审核<br> update u1 set F_kqty=isnull(F_kqty,0) - i1.F_qty,F_pqty=isnull(F_pqty,0) - i1.F_qty <br> from pub_stock u1,(select i1.F_wareID as F_shopID,t1.F_goodsID,sum(isnull(t1.F_qty,0)) as F_qty<br> from B_Csellsub t1,inserted i1,deleted d1<br> where t1.F_CsellID=i1.F_CsellID<br> and t1.F_CsellID=d1.F_CsellID<br> and isnull(i1.F_stateID,'')<>isnull(d1.F_stateID,'')<br> and isnull(d1.F_stateID,'')<>'6'<br> and isnull(i1.F_stateID,'')='3'<br> group by i1.F_wareID,t1.F_goodsID) i1 <br> where u1.F_goodsID=i1.F_goodsID <br> and u1.F_shopID=i1.F_shopID<br> end<br> else<br> begin <br> --反审核<br> update u1 set F_kqty=isnull(F_kqty,0) + i1.F_qty,F_pqty=isnull(F_pqty,0) + i1.F_qty <br> from pub_stock u1,(select i1.F_wareID as F_shopID,t1.F_goodsID,sum(isnull(t1.F_qty,0)) as F_qty<br> from B_Csellsub t1,inserted i1,deleted d1<br> where t1.F_CsellID=i1.F_CsellID<br> and t1.F_CsellID=d1.F_CsellID<br> and isnull(i1.F_stateID,'')<>isnull(d1.F_stateID,'')<br> and isnull(d1.F_stateID,'')='3'<br> and isnull(i1.F_stateID,'')<>'6'<br> group by i1.F_wareID,t1.F_goodsID) i1 <br> where u1.F_goodsID=i1.F_goodsID <br> and u1.F_shopID=i1.F_shopID<br> <br> end<br> end<br><br>end