注册 登录
编程论坛 SQL Server论坛

编写简单的触发器运行结果不行

那个梦 发布于 2007-11-08 08:59, 1214 次点击
为表T创建一触发器:当职称从“讲师”晋升为“副教授”时,岗位津贴自动增加500元:从“副教授”晋升为“教授”时,岗位津贴自动增加900元。

create trigger update_teacher6 on teacher
for update
as
if update(prof)
declare @tno_new char(6), @tno_old char(6),
@prof_new char(12), @prof_old char(12)
select @tno_new =tno,@prof_new=prof from inserted
select @tno_old =tno,@prof_old=prof from deleted
if exists(select * from deleted
where @prof_old='讲师')
begin
update teacher
set comm=comm+500
where tno=@tno_old
end

请教啊!!!!
4 回复
#2
那个梦2007-11-08 09:09
请大家来帮忙啊..
#3
purana2007-11-08 09:37

[CODE]create table tt(
id int,
name varchar(10),
prof varchar(20),
comm int
)
insert tt select 1,'小明','讲师',1500
union all select 2,'小强','教授',3900
union all select 3,'小李','副教授',2500
union all select 4,'小陈','普通教师',1000
go
create trigger tri_temp on tt
for update
as
begin
if @@rowcount>1
begin
rollback tran
raiserror ('每次只能更新一条信息',16,1)
return
end
declare @old_prof varchar(20),@new_prof varchar(20)
declare @id int

select @id=id,@old_prof=prof from deleted
select @new_prof=prof from inserted

if update(prof)
begin
if @old_prof='讲师' and @new_prof='副教授'
update tt set comm=comm+500 where id=@id
if @old_prof='副教授' and @new_prof='教授'
update tt set comm=comm+900 where id=@id
end
end
go
select * from tt
update tt set prof='教授' where id=3
select * from tt
drop trigger tri_temp
drop table tt[/CODE]

#4
那个梦2007-11-08 18:33
谢谢你的解答阿!
#5
good_good_study2007-12-17 09:41
begin
 if @@rowcount>1 begin
 rollback tran
 raiserror ('每次只能更新一条信息',16,1)
 return
 end

不太明白这几句话是什么意思
1