create table 药库
(
medician_name varchar(20),
medician_store int check(medician_store>0),
修改日期 datetime default getdate()
)
create table 入库清单
(
medician_name varchar(20),
medician_norms varchar(20),
medician_number int,
medician_unit char(10) default '盒',
medician_unitprice money,
medician_totalmoney money,
date datetime default getdate(),
businessman char(10),
P_incharge char(10),
accountant char(10),
Y_person char(10),
J_person char(10)
)
if object_id('Trigger1','TR')is not null
drop Trigger Trigger1;
go
create trigger Trigger1 on 入库清单
after insert
as
declare @number int,@name varchar(20);
select @number=medician_number,@name=medician_name from inserted
if(@name not in(select medician_name from 药库))
begin
insert into 药库
select @name,@number,date
from inserted
end
else
begin
insert into 药库
select inserted.medician_name,@number+medician_store,date
from inserted,药库
where 修改日期 = (select top 1 修改日期 from 药库 where medician_name=inserted.medician_name
order by 修改日期 desc)
end
go
insert into 药库(medician_name,medician_store)output inserted.* values('感冒冲剂',1000);
insert into 药库 (medician_name,medician_store)output inserted.* values('阿司匹林',500);
insert into 药库 (medician_name,medician_store)output inserted.* values('斯达舒',1500);
insert into 药库(medician_name,medician_store) output inserted.* values('整肠生',1200);
insert into 入库清单
values('整肠生','500g',300,'盒','15.0','4500',getdate(),'刘金宝','李金宝','汤','周杰','蔡')