一个触发器的问题

kseawolfk 2008-04-09 08:25:21
有一个表teacher结构如下
tid tname
1 abc
2 bcd
3 efg
4 klm
另有一个表结构course如下
tid tname ....
2 null
3 null
2 null
4 null
现在要把course的tname值更改成和teacher表中相关的值。例如
tid tname ....
2 bcd
3 efg
2 bcd
4 klm
请问该怎么写sql语句?另外还需要写一个触发器,保证course的tid

更新时tname也要更新,请问如下的触发器有什么错?
create trigger t1 on course
for update
as
update course set tname=(select tname from teacher inner

join inserted on teacher.tid=inserted.tid where

course.tid=teacher.tid)
结果会把除了要更新的行外记录里的tname都变成null。请高手指点

...全文
74 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
kseawolfk 2008-04-09
  • 打赏
  • 举报
回复
谢谢roy_88中国风,问题已经解决。update t1 set tname=t2.tname 即可
kseawolfk 2008-04-09
  • 打赏
  • 举报
回复
非常感谢中国风的回答,不过问题是我只需要更新course表当中的tname属性。
create trigger tr_course on course
for update
as
if update(tid)
begin
update t1 -- 这个地方好像要改update t1 set tname=....不知道该怎么写了。。。。
from
course t1 join teacher t2 on t1.tid=t2.tid
where
exists(select 1 from inserted where tid=t1.tid)
end
中国风 2008-04-09
  • 打赏
  • 举报
回复
update t2
set tname=t1.Name
from teacher t1,course t2
where
t1.tid=t2.tid

---------

create trigger tr_course on course
for update
as
if update(tid)
begin
update t1
from
course t1 join teacher t2 on t1.tid=t2.tid
where
exists(select 1 from inserted where tid=t1.tid)
end

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧