if object_id('tb') is not null drop table tb go create table tb(id int identity(1,1),商品名称 varchar(50),商品数量 int,商品出库价 decimal(18,2),商品成本价 decimal(18,2)) insert into tb select '笔记本',5,55,20 go create trigger tri_name on tb instead of update as begin if update(商品出库价) begin update a set a.商品出库价=case when b.商品出库价<b.商品成本价 then b.商品成本价 else b.商品出库价 end from tb a inner join inserted b on a.id=b.id end endupdate tb set 商品出库价=15 where id=1 select * from tb /* id 商品名称 商品数量 商品出库价 商品成本价 1 笔记本 5 20.00 20.00 */ update tb set 商品出库价=35 where id=1 select * from tb /* id 商品名称 商品数量 商品出库价 商品成本价 1 笔记本 5 35.00 20.00 */
create trigger my_trig on 表1 for update as update 表1 set 商品出库价 = t.商品成本价 from inserted t where 表1.商品名称 = t.商品名称 where 表1.商品出库价 < t.商品成本价 go
create trigger tri_g on 表1 for insert as begin update 表1 set 出库价=成本价 where 出库价<成本价 end
create trigger my_trig on 表1 instead of update as update a set a.出库价 = (case when b.出库价 < a.成本价 then a.成本价 else b.出库价 end) from 表1 a join inserted b on a.商品名称= t.商品名称 go
drop table tb
go
create table tb(id int identity(1,1),商品名称 varchar(50),商品数量 int,商品出库价 decimal(18,2),商品成本价 decimal(18,2))
insert into tb select '笔记本',5,55,20
go
create trigger tri_name
on tb
instead of update
as
begin
if update(商品出库价)
begin
update a set a.商品出库价=case when b.商品出库价<b.商品成本价 then b.商品成本价 else b.商品出库价 end
from tb a inner join inserted b on a.id=b.id
end
endupdate tb set 商品出库价=15 where id=1
select * from tb
/*
id 商品名称 商品数量 商品出库价 商品成本价
1 笔记本 5 20.00 20.00
*/
update tb set 商品出库价=35 where id=1
select * from tb
/*
id 商品名称 商品数量 商品出库价 商品成本价
1 笔记本 5 35.00 20.00
*/
as
update 表1 set 商品出库价 = t.商品成本价 from inserted t where 表1.商品名称 = t.商品名称 where 表1.商品出库价 < t.商品成本价
go
create trigger tri_g on 表1
for insert
as
begin
update 表1 set 出库价=成本价 where 出库价<成本价
end
instead of update
as
update a
set a.出库价 = (case when b.出库价 < a.成本价 then a.成本价 else b.出库价 end)
from 表1 a
join inserted b on a.商品名称= t.商品名称
go