4.库存基本表(book_store)
字段 类型 描述 要求
库存流水号(store_id) 整型 存书的流水号
图书编号(book_id) 字符型 图书的编号 不为空
图书数量(qty) 整型 此种书的数量 不可以小于零
入库日期(in_date) 日期类型 图书入库的日期 默认系统当前日期

5.售书基本表(book_sales)
字段 类型 描述 要求
售书流水号(sale_no) 整型 自增长
图书编号(book_id) 字符型 图书的编号 不可以为空
数量(qty) 整型 出售数量 不可以小于零
日期(out_date) 日期类型 图书出售的日期 默认系统当前日期
现在我想写一个触发器要求:
当卖出n本书后,把库存中相应的书减去n本。
如果库存中这本书的数量减为零,则自动删除这本书的记录。
请问各位高手这个触发器应该怎么写,谢谢

解决方案 »

  1.   

    CREATE TRIGGER tr_test ON [dbo].[book_sales] 
    FOR INSERT
    AS
    update book_store set qty=qty-(select qty from inserted) /*更新库存数量*/
    where book_id=(select book_id from inserted)CREATE TRIGGER tr_test ON [dbo].[book_store] 
    FOR Update
    AS
    if (select qty from inserted)=0
        delete from book_store where  store_id=(select store_id from inserted) /*删除*/
      

  2.   

    create trigger on book_sales
    for insert
    as
    begin
      declare @i int,@sl int
      declare @book_id varchar(20)
      select @book_id = book_id ,@sl = qty from inserted
      select @I = qty from book_store where book_id = @book_id
      if (@i - @sl) = 0
        delete from book_store where book_id = @book_id
      else
        update book_store  set qty = qty - @sl where book_id = @book_id
    end