回复 #1 JJooO 的帖子
这样理解楼主的意思,不晓得对不对?
写一个product表上的触发器
当修改表product的条件满足:update product set pNum=pNum-x where pid=a
就往sale表中写数据,写入数据的条数由 x 决定
Create Trigger TU_product on product
For Update
AS
declare @i_pId char(6), @i_pNnme varchar(300), @i_pPrice smallmoney, @i_pNum float
declare @d_pId char(6), @d_pNnme varchar(300), @d_pPrice smallmoney, @d_pNum float
declare @ftch_ins int, @ftch_del int
declare @x int, @i int
begin
declare c_product_ins cursor for select pId, pName, pPrice, pNum from inserted
open c_product_ins
fetch c_product_ins into @i_pId, @i_pNnme, @i_pPrice, @i_pNum
set @ftch_ins=@@fetch_status
declare c_product_del cursor for select pId, pName, pPrice, pNum from deleted
open c_product_del
fetch c_product_del into @d_pId, @d_pNnme, @d_pPrice, @d_pNum
set @ftch_del=@@fetch_status
while (@ftch_ins+@ftch_del=0)
begin
if (@i_pId=@d_pId) and (@i_pId=a) and (@d_pNum>@i_pNum)
and (@i_pNnme=@d_pNnme) and (@i_pPrice=@d_pPrice)
begin
set @x=ceiling(@d_pNum-@i_pNum)
set @i=1
while @i<=@x
begin
insert into sale values(...)
set @i=@i+1
end
end
fetch c_product_ins into @i_pId, @i_pNnme, @i_pPrice, @i_pNum
set @ftch_ins=@@fetch_status
fetch c_product_del into @d_pId, @d_pNnme, @d_pPrice, @d_pNum
set @ftch_del=@@fetch_status
end
close c_product_ins
deallocate c_product_ins
close c_product_del
deallocate c_product_del
end
|