1

Dear amazing programmers, I am trying to create trigger in my application database to delete rows in a table based on multiple condition and deletion in another table, and i could use some help here.

Here is my code for the trigger

CREATE TRIGGER RemoveAccountReports 
AFTER DELETE ON Accounts 
FOR EACH ROW 
WHEN Reports.Parent_Type = ACCOUNT 
BEGIN 
DELETE FROM Reports 
WHERE Parent_ID = OLD.Account_ID; 
END;

I need to delete from Reports table with every deletion in Accounts table where Reports.Parent_ID = Accounts.Account_ID.

But only if Reports.Parent_type = "ACCOUNT"

How can i achieve that ?

thanks in advance.

2 Answers 2

1

It looks to me that you are missing quotes around ACCOUNT; it should be:

WHEN Reports.Parent_Type = 'ACCOUNT'

(Note that single quotes should be used with strings.)

Sign up to request clarification or add additional context in comments.

1 Comment

Great! Please consider checking the accept button to accept the answer.
0

when you execute your statement you gonna have this error :

 no such column: Reports.Parent_Type

the Solution :

CREATE TRIGGER RemoveAccountReports 
AFTER DELETE ON Accounts 
FOR EACH ROW 
BEGIN 
DELETE FROM Reports 
WHERE Parent_ID = OLD.Account_ID AND  Reports.Parent_Type = 'ACCOUNT' ; 
END;

2 Comments

android.database.sqlite.SQLiteException: no such column: ACCOUNT (code 1): , while compiling: DELETE FROM Accounts WHERE Account_ID = 1
hey Please can you post your Class that extends SQLiteOpenHelper to see your Database Structutre and understand from where come this problem ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.