'Insert Into from another table causing Can't update 'table' in stored function/trigger because it is already used by statement which invoked

I'm pretty new to SQL so if you can keep it as as simple as possible that would be appreciated.

Currently trying to add all the contact IDs from the contact table (civicrm_contacts) to a new custom field table which is a button to show/hide a contact from the main website. At the moment whenever I run my query I get back the following:

"SQL Error [1442] [HY000]: Can't update table 'civicrm_contact' in stored function/trigger because it is already used by statement which invoked this stored function/trigger."

My query is going something like this:

INSERT INTO civicrm_value_appears_on_we_69 (entity_id)
SELECT cc.id 
FROM civicrm_contact cc
WHERE contact_type='Organization'

There are 3 columns in total for the 'civicrm_value_appears_on_we_69': 'ID', 'entity_id', and 'appears_on_website_45' which is a 0 or 1 for the button value. All I want is to insert the contact IDs for all the Organisation contacts, I don't mind about nulls for now as long as it makes the rows.

I've looked up a bit about the error and I'm unsure why it's happening. As far as I can gather this errors happpens when you're using a trigger that using the same table as you're inserting into/from. I'm not using a trigger at the moment, or perhaps I have this wrong.

Any suggestions?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source