SQL 数据库触发器问题 谢谢大大来回答

来源:百度知道 编辑:UC知道 时间:2024/05/28 02:29:00
表1字段: ID,总票数,状态
表2字段: ID,购买票数

表2的触发器上怎么实现在表2中插入一条记录,然后就:表1.[总票数]-表2.[购买票数]。 如果 购买票数<总票数,那么报错回滚,如果总票数=0,表1.状态值=1

谢谢大哥

create trigger test
for 表2 insert,update
as
if exists (select * from 表1,inserted from 表1.ID=inserted.ID and 总票数< 购买票数 or 表1.总票数=0)
begin
ROLLBACK;
print '总票小与购买票或票以卖完'
end
else
if exists(select * from 表1 where 表1.总票数=0)
update 表1 set 表1.状态='1' where 表1.总票数=0
else
update 表1 set 表1.总票数=表1.总票数-inserted.购买票数 from 表1,inserted where 表1.ID=inserted.ID

试下吧,你说 如果 购买票数<总票数,那么报错回滚 。应该是总票数< 购买票数 才回滚吧!