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

mssql触发器demo的更多相关文章

  1. 触发器Demo

    --mysql 触发器简单实例 --创建表1 )) ; --创建表2 )); --创建触发器,表一增加数据时,表二自动增加数据 create trigger t_afterinsert_on_tab1 ...

  2. MSSQL 触发器 暂停 和 启动

    开启关闭触发器 禁用: ALTER TABLE member DISABLE TRIGGER trig1 GO 恢复: ALTER TABLE member ENABLE TRIGGER trig1 ...

  3. MSSQL触发器

    1.触发器语法 CREATE TRIGGER<trigger name> ON [<模式名>.]<表名或视图名> [WITH ENCRYPTION] {{{FOR| ...

  4. mssql游标demo

    declare @billIds varchar(400) declare @billId varchar(40) DECLARE c1 CURSOR FOR select top 5 SaleNo ...

  5. mssql存储过程demo

    ALTER PROCEDURE [dbo].[sp_get_saleData]ASBEGIN set nocount on -- 获取最近上传数据的时间戳 declare @dd datetime s ...

  6. mssql函数demo

    ALTER FUNCTION [dbo].[f_GetCookType] (@saleDate datetime)RETURNS varchar(6)ASBEGIN declare @cookType ...

  7. SQL Server 触发器demo

      GO /****** Object: Trigger [dbo].[tri_device] Script Date: 2018/6/11 10:56:08 ******/ SET ANSI_NUL ...

  8. MSSQL 视图/事务(TRAN[SACTION])/存储过程(PROC[EDURE])/触发器(TRIGGER )

    --视图 视图是一张虚拟表,它表示一张表的部分数据或多张表的综合数据,其结构和数据是建立在对表的查询基础上 视图在操作上和数据表没有什么区别,但两者的差异是其本质是不同: 数据表是实际存储记录的地方, ...

  9. T4 代码生成 Demo (抽奖程序)

    参考自这位大狮的:  https://github.com/Pencroff/Dapper-DAL/blob/master/Dapper-DAL/Models/ModelGenerator.tt 项目 ...

随机推荐

  1. 配置SQL Server 2008服务器

    怎么配置SQL Server 2008服务器_百度经验 http://jingyan.baidu.com/article/9faa7231a922c1473c28cb23.html 1.验证安装是否成 ...

  2. RxJava学习( 二)

    1) Scheduler 的 API (一) 在RxJava 中,Scheduler ——调度器,相当于线程控制器,RxJava 通过它来指定每一段代码应该运行在什么样的线程.RxJava 已经内置了 ...

  3. WinAPI—— CallNextHookEx调用下一个钩子

    CallNextHookEx(   hhk: HHOOK;    {当前钩子的句柄}   nCode: Integer; {钩子代码; 就是给下一个钩子要交待的}   wParam: WPARAM; ...

  4. SpringMVC中对Controller使用AOP

    转自http://usherlight.iteye.com/blog/1306111 正确配置spring aop,在controller中使用AOP 在controller中使用AOP的问题主要在于 ...

  5. mysql系列命令解释

    mysqld - the MySQL server mysql - the MySQL command-line tool mysqlaccess - client for checking acce ...

  6. Ajax时代 SQL注入依然是隐患

    许多网站程序在编写时,没有对用户输入数据的合法性进行判断,使应用程序存在安全隐患.用户可以提交一段数据库查询代码(一般是在浏览器地址栏进行,通过正常的www端口访问),根据程序返回的结果,获得某些想得 ...

  7. jQuery Mobile 入门教程

    你每天都会对着它讲话,和它玩游戏,用它看新闻——没错,它就是你裤兜里的智能手机.android,黑莓还是iphone?为了让你清楚意识到究竟哪些才算是智能手机,我在下面总结了一个智能手机系统/设备的列 ...

  8. jquery页面无刷新切换皮肤并保存

    效果体验:http://runjs.cn/detail/hijgcghe <!DOCTYPE html> <html> <head> <meta http-e ...

  9. android Bitmap getByteCount和getRowBytes

    今天做图像缓存需要计算Bitmap的所占的内存空间,于是研究了下Bitmap关于内存占用的API 1.getRowBytes:Since API Level 1,用于计算位图每一行所占用的内存字节数. ...

  10. 使用DialogFragment创建对话框总结

    回调Activity中的函数 http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents 在DialogFragme ...