怎样写在一定条件下才能触发的触发器?

jjja 2004-12-27 03:30:19
假设有两个表table1,table2,着里的出发条件有两种情况:
1)当table1中的某两个字段a,b中有一个被update或者同时被update,则把update的数据写入table2中同名字段;
2)只有当table1的字段A被update并且,字段B的值是‘NEW’时才能出发,把A字段写入table2的同名字段。
请高人执教,这两个触发器该怎么写?
...全文
146 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jjja 2004-12-28
  • 打赏
  • 举报
回复
搞定了,谢谢各位!
下面是我写的,希望对后来人有所帮助:
IF UPDATE(userId) OR UPDATE(password_)
if exists(select * from inserted where passwordEncrypted=0)
begin
insert into User_change_info
(A,
B,
C,
D,
E
) select
ins.A,
ins.B,
del.C,
ins.E + ins.F + ins.G,
'test',
from inserted ins,deleted del;
end
vinsonshen 2004-12-27
  • 打赏
  • 举报
回复
如何取update前的字段的值呢:


select * from deleted
jjja 2004-12-27
  • 打赏
  • 举报
回复
如何取update前的字段的值呢?用“:old.A”?好像不行也
jixinming 2004-12-27
  • 打赏
  • 举报
回复
create trigger 名字 on table
for update
as
if not exists(select * from inserted where B='NEW')
rollback

insert ......
jjja 2004-12-27
  • 打赏
  • 举报
回复
是这样的,假设table1里有两个字段A,B;table2里有三个字段A2,B2,oldA:
如果table1发生了update,且字段B是‘NEW’,则把新的table1的A,B字段的值写入table2的A,B字段,并且把table1字段A update以前的值写入table2的oldA字段。
zjcxc 元老 2004-12-27
  • 打赏
  • 举报
回复
你要说明你想做些什么才行啊

按你目前给的信息,只能这样回答:

if update(A) --如果更新了A
and exists(select * from inserted where B='NEW') --如果字段B是NEW
....--你的处理
jjja 2004-12-27
  • 打赏
  • 举报
回复
谢谢各位!
对于第2个问题,如果触发条件改为只有table1的字段B的值是‘NEW’,并且A字段被更新时,才进行触发操作,该怎么写?
也就是说先要判断一下字段B的值,然后再判断A是否被update该怎么写?
yingqing 2004-12-27
  • 打赏
  • 举报
回复
if update(a)
insert .... select xxx from inserted where 字段B = 'NEW'
zjcxc 元老 2004-12-27
  • 打赏
  • 举报
回复
2:
if update(a)
insert .... select xxx from inserted where 字段B = 'NEW'
txlicenhe 2004-12-27
  • 打赏
  • 举报
回复
1:
if update(a) or update(b)
......

2:
if update(a)
insert .... where 字段B = 'NEW'

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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