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. mysql学习链接

    1 传智播客PHP培训.刘道成.PHP视频教程.mysql http://down.51cto.com/zt/887

  2. http://jingyan.baidu.com/article/a378c960630e61b329283045.html

    http://jingyan.baidu.com/article/a378c960630e61b329283045.html

  3. 李洪强iOS开发之图片拉伸技巧

    纵观移动市场,一款移动app,要想长期在移动市场立足,最起码要包含以下几个要素:实用的功能.极强的用户体验.华丽简洁的外观.华丽外观的背后,少不了美工的辛苦设计,但如果开发人员不懂得怎么合理展示这些设 ...

  4. *J2EE中乱码处理

    发生中文乱码有三种情况 表单form (1)post 首先确定浏览器的编码方式,比如说utf-8,请求发给web服务器,web服务器以编码方式iso-9959-1来接收数据(服务器是外国人编写的),服 ...

  5. 算法总结之欧拉函数&中国剩余定理

    算法总结之欧拉函数&中国剩余定理 1.欧拉函数 概念:在数论,对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目. 通式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)( ...

  6. POJ3278——Catch That Cow(BFS)

    Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...

  7. mysql concat和group_concat

    mysql concat(str1,str2...)连接两个字符串,(数字也是可以的,会转成字符串) MySQL的concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL mys ...

  8. java ServerSocket服务端编程

    public class Test { public static void main(String[] args) throws Exception{ //1. 构造ServerSocket实例,指 ...

  9. WinAPI——钩子函数大全2

    CallNextHookEx 函数功能:该函数发送挂钩信息给当前挂钩链中的下一个挂钩处理过程,一个挂钩处理过程可在对该挂钩信息进行处理之前或之后调用本函数. 函数原形:LRESULT CallNext ...

  10. POI 中Cell的backgroundcolor和foregroundcolor

    刚开始以为要获得cell的背景色是使用  getFillBackgroundColor()这个函数(这里返回的是调色板的索引,要获得RGB需要先获得系统的Pallete,然后在获得 RGB).结果出来 ...