求写一个update触发器

来源:百度知道 编辑:UC知道 时间:2024/06/10 08:33:37
两张表主表:drug_info,从表: cpa_drug。其中主表的pzwh和从表的batch_no字段相关联。主表的pzwh字段改变的时候从表中的bathc_no字段也要改变,并且当主表xh_code的字段改变的时候从表中frug_no的字段也要改变,保留没有改变的数据!怎么写触发器啊?我写了个存储过程,不过,查询速度无法令我满意!查了十多分钟都没查出来!各位救救我吧!
create procedure pro_gxcd
as

update testcpa_drug
set testcpa_drug.drug_code=testdrug_info.xh_code
from testdrug_info
where testdrug_info.pzwh=testcpa_drug.standard_id

go

create trigger test
on drug_info
for update
as
declare @oldno int
declare @newno int
declare @oldcode int
declare @newcode int
select @oldno=pzwh,@oldcode=xh_code from deleted
select @newno=pzwh,@newcode=xh_code from inserted
if @oldno<> @newno
begin
update cpa_drug set bathc_no=@newno where bathc_no=@oldno
end
if @oldcode<> @newcode
begin
update cpa_drug set frug_no=@newcode where frug_no=@oldcode
end

go

前触发器吧