<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Technology: Learn and Share &#187; MySQL Triggers</title>
	<atom:link href="http://crazytoon.com/category/mysql-triggers/feed/" rel="self" type="application/rss+xml" />
	<link>http://crazytoon.com</link>
	<description>Enterprise level solutions, LAMP, Linux, Apache, MySQL, PHP, Perl, Windows, Cache, Optimization</description>
	<lastBuildDate>Fri, 16 Jul 2010 20:24:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MySQL: Solution for ERROR 1442 (HY000): Can&#8217;t update table &#8216;t1&#8242; in stored function/trigger because it is already used by statement which invoked this stored function/trigger.</title>
		<link>http://crazytoon.com/2008/03/03/mysql-error-1442-hy000-cant-update-table-t1-in-stored-functiontrigger-because-it-is-already-used-by-statement-which-invoked-this-stored-functiontrigger/</link>
		<comments>http://crazytoon.com/2008/03/03/mysql-error-1442-hy000-cant-update-table-t1-in-stored-functiontrigger-because-it-is-already-used-by-statement-which-invoked-this-stored-functiontrigger/#comments</comments>
		<pubDate>Tue, 04 Mar 2008 02:42:02 +0000</pubDate>
		<dc:creator>Tracy</dc:creator>
				<category><![CDATA[CentOS]]></category>
		<category><![CDATA[Enterprise level solutions]]></category>
		<category><![CDATA[Linux Apache MySQL PHP]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL Triggers]]></category>

		<guid isPermaLink="false">http://crazytoon.com/2008/03/03/mysql-error-1442-hy000-cant-update-table-t1-in-stored-functiontrigger-because-it-is-already-used-by-statement-which-invoked-this-stored-functiontrigger/</guid>
		<description><![CDATA[Here is a sample table you can create to test following problem/solution on:
CREATE TABLE `t1` (
`a` char(1) default NULL,
`b` smallint(6) default NULL
);
insert into t1 values ('y','1');
I have a table t1 which has column a and b, I want column a to be updated to &#8216;n&#8217; when column b = 0. Here is the first version [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a sample table you can create to test following problem/solution on:</p>
<p><code>CREATE TABLE `t1` (<br />
`a` char(1) default NULL,<br />
`b` smallint(6) default NULL<br />
);<br />
insert into t1 values ('y','1');</code></p>
<p>I have a table t1 which has column a and b, I want column a to be updated to &#8216;n&#8217; when column b = 0. Here is the first version I created:</p>
<p><code>DELIMITER |<br />
CREATE TRIGGER trigger1 AFTER UPDATE ON t1<br />
FOR EACH ROW UPDATE t1 SET a= 'n' WHERE b=0;<br />
|<br />
DELIMITER ;</code></p>
<p>The trigger created successfully but I got this error when I tried to do an update on column b on table t1:<br />
<code>mysql&gt; update t1 set b=0;<br />
ERROR 1442 (HY000): Can't update table 't1' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.<br />
</code></p>
<p>After searching online for a while and trying different solutions, I finally found a way to update the table which has trigger on it:<br />
<code><br />
drop trigger trigger1;<br />
DELIMITER |<br />
CREATE TRIGGER trigger1 BEFORE UPDATE ON t1<br />
FOR EACH ROW<br />
BEGIN<br />
IF NEW.b=0 THEN<br />
SET NEW.a = 'n';<br />
END IF;<br />
END<br />
|<br />
DELIMITER ;</code></p>
<p>After the new trigger is in, I issued the same update query and &#8220;<small>ERROR 1442 (HY000): Can&#8217;t update table &#8216;t1&#8242; in stored function/trigger because it is already used by statement which invoked this stored function/trigger.</small>&#8221;  didn&#8217;t show up and it updated the col a value to &#8220;n&#8221; as it suppose to.</p>
<p><code>mysql&gt; update t1 set b=0;<br />
Query OK, 1 row affected (0.01 sec)<br />
Rows matched: 1  Changed: 1  Warnings: 0<br />
mysql&gt; select * from t1\G<br />
*************************** 1. row ***************************<br />
a: <strong>n</strong><br />
b: 0</code></p>
<p>Therefore, if you want to create a trigger on the table which will update itself, make sure you use the NEW.column_name to refer to the row after it&#8217;s updated and don&#8217;t do the full update statement!</p>
<p>However, if you are updating some other table, then you can use the regular update statement:</p>
<p><code>DELIMITER |<br />
CREATE TRIGGER trigger1 AFTER UPDATE ON <strong>t1 </strong><br />
FOR EACH ROW UPDATE<strong> t2 </strong>SET a= 'n' WHERE b=0;<br />
|<br />
DELIMITER ;</code></p>
<p>————————————-<br />
<small>DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. <strong>Use at your own risk</strong>.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2008/03/03/mysql-error-1442-hy000-cant-update-table-t1-in-stored-functiontrigger-because-it-is-already-used-by-statement-which-invoked-this-stored-functiontrigger/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>
