zoukankan      html  css  js  c++  java
  • mssql触发器demo

    USE [pos]
    GO
    /****** Object: Trigger [dbo].[tr_insert] Script Date: 06/26/2014 09:27:19 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    -- =============================================
    -- Author: <Author,,Name>
    -- Create date: <Create Date,,>
    -- Description: <Description,,>
    -- =============================================
    ALTER TRIGGER [dbo].[tr_insert]
    ON [dbo].[bill_d]
    AFTER INSERT
    AS
    BEGIN
    declare @goodsCode varchar(5)
    declare @qty decimal(18,2)
    declare @price money
    declare @cnt int
    declare @billType varchar(20)
    declare @billNo varchar(20)
    declare c1 cursor for select goodsCode,qty,price,billNo from inserted
    open c1
    FETCH NEXT FROM c1 INTO @goodsCode,@qty,@price,@billNo
    WHILE @@FETCH_STATUS =0
    begin
    select @billType=billType from bill_m where billNo=@billNo
    if @billType='StockIn' begin -- 进货
    select @cnt=isnull(count(goodscode),0) from goods_stock where goodscode=@goodscode
    if @cnt=0 begin
    insert into goods_stock(goodscode,stock,price) values (@goodscode,@qty,@price)
    end else begin
    update goods_stock set stock=stock+@qty,price=(stock*price+@qty*@price)/(stock+@qty)
    where goodscode=@goodscode
    end
    end else if @billType='StockInReturn' begin -- 进货退货
    select @cnt=isnull(count(goodscode),0) from goods_stock where goodscode=@goodscode
    if @cnt>0 begin
    update goods_stock set stock=stock-@qty where goodscode=@goodscode
    end
    end
    FETCH NEXT FROM c1 INTO @goodsCode,@qty,@price,@billNo
    end
    close c1
    deallocate c1
    END

  • 相关阅读:
    N25_复杂链表的复制
    N24_二叉树中和为某一路径
    N23_判断是否为二叉搜索树的后序遍历序列
    N22_从上到下打印二叉树
    win7桌面小工具已停止工作解决办法
    C3P0数据库连接池使用
    js中的页面跳转
    怎么用js代码禁止浏览器的前进与后退?
    怎么在 Dos 下运行 PHP 和 MySQL 命令
    80端口被system 占用解决方法
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/3809394.html
Copyright © 2011-2022 走看看