本文介绍了尝试创建新触发器时出现SQL错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试创建触发器时:
And when I try to create a trigger:
CREATE TRIGGER ctg_el_del
AFTER DELETE ON ctg
FOR EACH ROW
BEGIN
DELETE FROM ctg
where ctg.id_ctg = old.lft
WHERE cat.id_ctg = old.rgt;
END;
此错误显示:
ERROR: syntax error at or near "BEGIN"
LINE 4: BEGIN
^
推荐答案
STEP 1:您需要创建返回类型为Trigger的函数,在该函数中应提及如下的SQL操作:
STEP 1 : You need to Create function that returns type Trigger in that function you should mention the SQL Operation like below:
CREATE FUNCTION trigger_function() RETURNS TRIGGER AS $$
BEGIN
DELETE FROM ctg
WHERE ctg.id_ctg = old.lft
AND ctg.id_ctg = old.rgt;
END
$$
LANGUAGE plpgsql;
第2步:然后在表上创建触发器以在之后调用像这样删除:
STEP 2 : And then create the trigger on the table to call after delete like this:
CREATE TRIGGER ctg_el_del
AFTER DELETE ON ctg
FOR EACH ROW
EXECUTE PROCEDURE trigger_function();
这篇关于尝试创建新触发器时出现SQL错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!