首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

数据库触发器解决思路

2013-01-01 
数据库触发器Goods(g_id char(10) , g_quantity int )Idents(i_id int,g_id char(10))//g_id 是Goods的外

数据库触发器
Goods(g_id char(10) , g_quantity int )
Idents(i_id int,g_id char(10))  //g_id 是Goods的外码
 
create trigger quantity_enough
on Ident //订单表
for insert,update
as
  declare @quantity int  //商品表的商品数量
  select @quantity=g_quantity from Goods where g_id=(select g_id from inserted)
  if @quantity<=0
    begin 
      print '商品数量不足,不能购买!操作已取消!'
      rollback transaction
    end
  else
    update Goods set g_quantity=@quantity-1 where g_id=(select g_id from inserted)

当我在订单表中插入一条记录时,只要有一个商品的数量不足,我就不能购买,
如果数量都足,商品表的商品数量都减去1,怎么回事?

[解决办法]
select g_id from inserted

把这个结果集打印出来看看是什么

热点排行