无忧岛

07 十, 2008

献给想学习SQL触发器的朋友

Posted by: kinglife In: SQL Server

前些天写的库存管理模块,因为需要看了些网上的SQL SERVER的触发器的教程,但是因为我的模块涉及的表关系比较杂,CSDN的朋友建议用存储过程去实现,所以取消了那个触发器,改用了存储过程,现在把触发器的全部内容贴出来给新学触发器的朋友参考,详细的注释都写了,有问题可以在下面留言,我会尽快回复的.

/* 商品采购表触发器 开始 Create By KingLife 2008.09.25*/
If exists(select name from sysobjects where name='ProductDB' and type='tr')
Drop Trigger ProductDB
go
create trigger ProductDB
on Best_BuyInfo
for insert,update,delete
as
declare @ProductID nvarchar(10) --商品ID
declare @ProductCount int --存放数量或者差值
declare @NewPro nvarchar(10) --商品名称更改之用
declare @OldPro nvarchar(10)
begin
if @@rowcount=0
return --沒有影响记录就返回
if exists(select 1 from inserted) and exists(select 1 from deleted)
--更新数据
begin
set @ProductID=(select product from Inserted) --更新商品的ID
set @ProductCount=(select Count from Inserted)-(select count from Deleted) --计算数量更改前后差值
--
if @ProductCount=0
--
return --如果数量没有变化表示更新的不是数量字段 返回
if update([Count]) --更新的是数量
begin
if(isnull((select product from Best_ProductBase where Product=@ProductID),0))=0 --判断是否存在该商品
begin --当前库中不存在更新的商品时 把采购表中的该类商品ID 和 总量 Insert
set @ProductCount=(select sum([count]) from Best_BuyInfo where Product=@ProductID and IsReturn=0) --获取该商品的总量
execute ('insert into Best_ProductBase(Product,Count) values('''+ @ProductID +''','+ @ProductCount +')')
end
else --库存表中存在此商品
begin --计算方法 仓库中该商品数量 + 差值 =现有数量
execute ('update Best_productBase set count=Count + '+ @ProductCount +' where product='''+ @ProductID +'''')
if(select Count from Best_ProductBase where Product=@ProductID)<0
begin --出现负数的置零
update best_Product set [count]=0 where product=@ProductID
end
end
end
else if update([Product]) --更新的是商品ID而不是数量
begin
set @NewPro=(select product from inserted)
set @OldPro=(select product from deleted)
set @ProductCount=(select [Count] from Deleted)
exec('update Best_ProductBase set [Count]=[Count]+ '+ @ProductCount +' where Product='''+ @NewPro +'''')
exec('update Best_ProductBase set [Count]=[Count]- '+ @ProductCount +' where Product='''+ @OldPro +'''')
end
else if update([IsReturn]) --退货处理
begin
set @ProductID=(select product from Inserted)
set @ProductCount=(select [Count] from Inserted)
if(select IsReturn from Inserted)=1 --由退货状态改为订单状态
begin
execute ('update best_productbase set [count]=[count]- '+ @ProductCount +' where product='''+ @ProductID +'''')
end
else if(select IsReturn from Inserted)=0 --由订单状态改为退货状态
begin
execute ('update best_productbase set [count]=[count]+ '+ @ProductCount +' where product='''+ @ProductID +'''')
end
 
end
else --更新了其它内容返回
return
end
 
else if exists(select 1 from inserted)
begin
--新增数据
set @ProductID=(select Product from Inserted) --获取商品ID
set @ProductCount=(select count from Inserted) --获取新增商品数量
if(isnull((select product from Best_ProductBase where Product=@ProductID),0)=0 )
begin -- 如果当前仓库中不存在该商品则 insert into
set @ProductCount=(select sum([count]) from Best_BuyInfo where Product=@ProductID and isreturn=0) --仓库中不存在该商品则到采购表中汇总此商品的总量
execute ('insert into Best_ProductBase(product,Count) values('''+ @ProductID +''','+ @ProductCount +') ')
end
else
begin --正常添加 计算方法 仓库中当前该类商品数量+新记录的该类商品的数量=现有数量
update Best_ProductBase set [count]=[count]+@ProductCount where Product=@ProductID
end
end
else -- if exists(select 1 from deleted)
begin
--删除数据
set @ProductID=(select Product from Deleted) --删除记录的采购商品的ID
set @ProductCount=(select [count] from deleted) --删除记录的采购商品的数量
if(isnull((select product from Best_ProductBase where Product=@ProductID),0))=0 --判断库存表中是否存在该商品
begin --不存在此商品则把采购表中的数量汇总并 Insert
set @ProductCount=(select sum([count]) from Best_BuyInfo where product=@ProductID and isReturn=0)
execute ('insert into Best_ProductBase(Product,Count) values('''+ @ProductID +''','+ @ProductCount +')')
end
else
begin --计算方法 当前仓库中的该类商品数量-删除的商品数量=现有数量
update best_productBase set [count]=[count]-@ProductCount where product=@ProductID
if(select Count from Best_ProductBase where Product=@ProductID)<0 --出现负数的置零
begin
update best_Product set [count]=0 where product=@ProductID
end
end
end
end
 
/* Under the row Just For test
 
select * from best_buyinfo where product='01'
 
About Trigger
 
it has two temp tables : Inserted,Updated
1.插入操作(Insert)
Inserted表有数据,Deleted表无数据
 
2.删除操作(Delete)
Inserted表无数据,Deleted表有数据
 
3.更新操作(Update)
Inserted表有数据(新数据),Deleted表有数据(旧数据)
 
if @@rowcount=0
return--沒有影响记录就返回
 
if exists(select 1 from inserted) and exists(select 1 from deleted)
--更新
else if exists(select 1 from inserted)
--新增
else --刪除
 
*/

注意:在SQL语句中如果有 If else 这样的逻辑判断结构,请用 Begin end 把内容包含进去,另外注意缩进,让代码清晰,也方便维护. 如下

declare @x int
declare @y int
set @x=1
set @y=2
If @x<@y
begin
print 'Max Number is Y'
end
else if @x>@y
begin
print 'Max Number is X'
end
else
begin
print 'x = y'
end

版权所有,转载时必须以链接形式注明作者和原始出处及本声明:KingLife@无忧岛

本文链接: http://www.islandcn.com/post/111.html



No Responses to "献给想学习SQL触发器的朋友"

Comment Form

Categories

Flickr PhotoStream

    flickrRSS probably needs to be setup

About

Name:KingLife
Email:lifewz#163.com