Magdi Saeed
Magdi Saeed

Reputation: 11

PL/SQL 1064 error at line 5 in delete trigger

I am trying to create a trigger to delete articles after it deletes of all of its article contents. I am getting a 1064 error at line 5.

Here is the PL/SQL I have written:

delimiter |

create trigger `cleanup_article` after delete on `article_content`
for each row
begin
    declare x int;
    set x = select count(*) as x from `article_content` where id = old.id;
    if x = 0 then
        delete from `article` where id=old.id;
    end if;
end |

delimiter ;

Upvotes: 1

Views: 169

Answers (1)

Jeffrey Kemp
Jeffrey Kemp

Reputation: 60262

There's a few problems there, due to invalid syntax for PL/SQL:

  1. identifiers are either undelimited or delimited using ", not `

  2. declare must come before begin

  3. set is not a valid keyword. To get the count, use INTO, e.g. SELECT COUNT(*) INTO x FROM ...

  4. The DELETE statement refers to old.id, this should be :old.id

  5. What's with the "delimiter" statements? Just end the trigger definition with ;

Upvotes: 1

Related Questions