如何区分sql中的触发动作?

问题描述 投票:0回答:1

我想创建在插入、更新和删除时触发的触发器。 在触发器内部,我需要以不同的方式处理这些操作。 我怎样才能找出哪个动作触发了触发器? 或者,我应该对不同的操作使用多个触发器吗?

'if update(FIRSTNAME)' 工作正常,但如何区分插入和删除? 'if insert(FIRSTNAME)' 显然不起作用。

ALTER TRIGGER "FR_CHANGES" AFTER INSERT, DELETE, UPDATE
ORDER 1 ON "DBA"."PERSONAL"
referencing old as old_rec new as new_rec
FOR EACH ROW
BEGIN
    if update(FIRSTNAME) or update(LASTNAME) then
       insert into FR_CHANGES (PNR_PERSONAL, CHANGE_TYPE) values (old_rec.PNR, "update")  
    end if
    if insert(FIRSTNAME) then //this line gives an error
       insert into FR_CHANGES (PNR_PERSONAL, CHANGE_TYPE) values (new_rec.PNR, "insert")  
    endif
END 
sql triggers sybase sqlanywhere
1个回答
0
投票

可以通过插入/删除虚拟表中行的存在来区分,像这样:


IF EXISTS (select * from inserted) AND NOT EXISTS (select * from deleted)
BEGIN

print 'actions after insert statement'


END
ELSE IF EXISTS (select * from inserted) AND EXISTS (select * from deleted)
BEGIN


print 'actions after update statement'

END

ELSE IF EXISTS (select 1 from deleted) AND NOT EXISTS (select 1 from inserted)
BEGIN

print 'actions after delete statement'

END

© www.soinside.com 2019 - 2024. All rights reserved.