你好,你的问题可以用触发器来(这个代码可以根据你的具体情况来适当的修改,假定你的表名是[employee])。
实现代码如下:
程序代码:
----------定义表结构
if exists (select * from sys.tables where object_id = object_id(N'[dbo].[employee]') and type in (N'U'))
drop table [dbo].[employee]
go
set ansi_nulls on
go
set quoted_identifier on
go
create table EMPLOYEE
(
EmpID varchar(50),
DepID varchar(50),
Title varchar(50),
EmpTBID varchar(50),
EmpTCID int,
TRP float
)
go
----插入测试数据----------------
delete from EMPLOYEE
insert EMPLOYEE values('001','总经办','总经理',null,0,100)
insert EMPLOYEE values('002','营销中心','副总经理','001',1,100)
insert EMPLOYEE values('004','财务中心','财务总监','001',1,100)
insert EMPLOYEE values('006','财务部','财务经理','004',2,100)
insert EMPLOYEE values('007','IT部','IT经理','004',2,100)
insert EMPLOYEE values('008','财务部','出纳','006',3,100)
insert EMPLOYEE values('009','财务部','成本会计','006',3,100)
insert EMPLOYEE values('010','财务部','总账会计','006',3,100)
insert EMPLOYEE values('011','财务部','材料会计','006',3,100)
insert EMPLOYEE values('012','IT部','系统管理员','007',3,100)
----定义update触发器-----
if exists(select * from sys.triggers where name = N'TR_Table_Update')
drop trigger TR_Table_Update
go
create trigger TR_Table_Update
on EMPLOYEE
after update
as
begin
if update ([TRP])--当TRP字段被更新时,才会触发此触发器
begin
declare @totalBeforeUpdate float--更新前TRP的值
select @totalBeforeUpdate=[TRP] from deleted
declare @totalAfterUpdate float--更新后TRP的值
select @totalAfterUpdate=[TRP] from inserted
declare @total float --增加的值
set @total=@totalAfterUpdate-@totalBeforeUpdate
if @total=10 --当出纳的TRP字段增加10分时执行(根据情况可以自己修改)
begin
declare @total_CW int--财务人数
declare @total_IT int--IT人数
declare @total_ZJL int--总经理人数
select @total_CW=count(DepID) from EMPLOYEE where DepID='财务部'
select @total_IT=count(DepID) from EMPLOYEE where DepID='IT部'
select @total_ZJL=count(Title) from EMPLOYEE where Title!='总经理'
update EMPLOYEE set TRP=TRP+@total/@total_CW
where Title='财务经理'
update EMPLOYEE set TRP=TRP+@total/(@total_CW+@total_IT)
where Title='财务总监'
update EMPLOYEE set TRP=TRP+@total/@total_ZJL
where Title='总经理'
end
end
end
go
----显示更新之前的数据--------------------
select * from EMPLOYEE
----执行更新操作,并触发触发器进行相应的操作。-------------------------------------
update EMPLOYEE set TRP=TRP+10 where Title='出纳'
----显示更新之后的数据-------------------------------------
select * from EMPLOYEE
---------------------------------------
测试结果:
以上代码你可以测试一下是否能解决你的问题。
觉得好的话请将悬赏留下,我是冲着你的悬赏来的,你懂得!!!
[
本帖最后由 303770957 于 2013-9-9 10:27 编辑 ]