不把inserted里面的字段富裕变量@name等,直接通过inserted. medician_name 引用试试
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(),'刘金宝','李金宝','汤唯','周','蔡')
if object_id('Trigger1','TR')is not null drop Trigger Trigger1; go create trigger Trigger1 on 入库清单 for insert as declare @number int,@name varchar(15); select @number=medician_number,@name=medician_name from inserted if(@name not in(select medician_name from 药库)) begin insert into 药库(medician_name,medician_store,修改日期) select @name,@number,date from inserted end else begin update 药库 set medician_store=medician_store+(select medician_number from inserted), 修改日期=getdate() where medician_name =(select medician_name from inserted ) end go
if object_id('Trigger1','TR')is not null drop Trigger Trigger1; go create trigger Trigger1 on 入库清单 for insert as declare @number int,@name varchar(15); select @number=medician_number,@name=medician_name from inserted if(@name not in(select medician_name from 药库)) begin insert into 药库(medician_name,medician_store,修改日期) select @name,@number,date from inserted end else begin insert into 药库 select medician_name,medician_number,date from inserted end go
if object_id('Trigger1','TR')is not null drop Trigger Trigger1; go create trigger Trigger1 on 入库清单 after insert as declare @number int,@name varchar(15); select @number=medician_number,@name=medician_name from inserted if(@name not in(select medician_name from 药库)) begin insert into 药库(medician_name,medician_store,修改日期) select @name,@number,date from inserted end else begin insert into 药库 select @name,@number+medician_store,date from inserted,药库 where 修改日期 = (select top 1 修改日期 from 药库 where medician_name=@name order by 修改日期 desc) end go