我库存表字段包括
备件号,库存量,单价,日期
消耗表包括
备件号,消耗数量,单价
订购表包括
备件号,订购数量,单价消耗表中有触发器,当插入一条数量,库存量就减去消耗数量问题是
我消耗时,数量输入错误,需要修改,
怎样创建这个修改的触发器才能保证库存的数量正确!
备件号,库存量,单价,日期
消耗表包括
备件号,消耗数量,单价
订购表包括
备件号,订购数量,单价消耗表中有触发器,当插入一条数量,库存量就减去消耗数量问题是
我消耗时,数量输入错误,需要修改,
怎样创建这个修改的触发器才能保证库存的数量正确!
解决方案 »
- 求一个非常简单的mysql存储过程!!! mysql 的
- 小问题 帮忙、、、
- 推荐一款sql编辑器,支持同时操作多个(异构)数据库
- 怎样用一个SQL语句来表达百分比问题?(圣诞疯狂送分)
- 百分,xp_cmdshell使用问题,急!
- 求大神看看这两条相似的SQL语句 为什么执行效率相差这么大。。。。求大神帮助,小弟实在理解不了
- 一个关于时间和空值得问题
- 休息日与放假日同一天,要调整休息日。。。在线等。。。
- 谁知道sqlserver2000 evaluate edition 版本的使用期限是多长时间?
- 一个关于sql server 存储过程参数遍历的问题?
- 无法打开AdomdConnection??
- 触发器问题!触发器可以指定在修改某个字段时触发吗?
也可以分开来写
create table t1(id int identity(1,1),type int, num int)create table t2(type int, num int)insert into t1 (type,num)
select 1,3
union
select 3,3
union
select 2,3
union
select 4,3
insert into t2(type,num)select 1,3
union
select 2,3union
select 3,3
union select 4,3
--setp 2
create trigger [dbo].[tr_table1] on [dbo].[t1]
for insert
as
begin
--select * into ace from inserted
update t2 set num = a.num+b.num from t2 a inner join inserted b on a.type=b.type
end
--setp 3
create trigger [dbo].[tr_table1_del] on [dbo].[t1]
instead of delete
as
begin
update t2 set num = a.num-b.num from t2 a inner join deleted b on a.type=b.type
delete from t1 where id =(select id from deleted)
end--setp 4
create trigger [dbo].[tr_table1_update] on [dbo].[t1]
instead of update
as
begin
declare @temp int
set @temp =0
select @temp = a.num-b.num from t1 a inner join inserted b on a.type=b.type
update t2 set num =num- @temp from t2 where type =(select type from inserted)
update t1 set num = b.num from t1 a inner join inserted b on a.id = b.id
endinsert into t1(type,num)
select 1, 17
select * from t1
select * from t2delete t1 where id =5
select * from t1
select * from t2update t1 set num =2 where id =4select * from t1
select * from t2