<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title>Retour à La Source • Tag: MySQL 5 • RSS</title>
		<link>http://www.ab-d.fr/tag/MySQL 5/</link>
		<description>Retour à La Source • Tag: MySQL 5 • RSS</description>
		<copyright>http://www.ab-d.fr/tag/MySQL 5/ (c) 2010</copyright>
		<language>fr</language>
		<pubDate>Mon, 06 Sep 2010 02:57:24 +0100</pubDate>
		<generator>Retour à La Source • Tag: MySQL 5 • RSS • version 1.0</generator>
		<webMaster>noreply@ab-d.fr ( Benoit Asselin )</webMaster>
		<image>
			<url>http://www.ab-d.fr/images/rss.gif</url>
			<title>Retour à La Source • Tag: MySQL 5 • RSS</title>
			<link>http://www.ab-d.fr/tag/MySQL 5/</link>
			<width>90</width>
			<height>30</height>
		</image>

		<item>
			<title>Comment créer une procédure stockée ? ( MySQL, SQL )</title>
			<link>http://www.ab-d.fr/date/2010-03-17/</link>
			<pubDate>Wed, 17 Mar 2010 15:45:00 +0100</pubDate>
			<author>noreply@ab-d.fr ( Benoit Asselin )</author>
			<category><![CDATA[MySQL]]></category>
			<category><![CDATA[MySQL 5]]></category>
			<category><![CDATA[SQL]]></category>
			<description><![CDATA[<p>
Une procédure stockée est un ensemble d'instructions SQL pré-compilées stockées sur le serveur de base de données.
</p>
<p>
L'intérêt réside surtout dans la rapidité en évitant les échanges avec le client, et la simplification du code lors de l'appel.
</p>
<p>
Voici un exemple qui contrôle le stock de produits disponible et qui réajuste automatiquement la table des quantités si cette dernière est supérieure au stock disponible :
</p>

<pre>
delimiter |

DROP PROCEDURE IF EXISTS CHECK_STOCKS |
CREATE PROCEDURE CHECK_STOCKS(p_order_id INT)
BEGIN
	DECLARE v_done INT DEFAULT 0;
	DECLARE v_o_p_id, v_quantity, v_stock INT;
	DECLARE v_cur1 CURSOR FOR
		SELECT opt.o_p_id , opt.o_p_quantity , p.product_stock
		FROM order_products_tmp opt , products p
		WHERE opt.order_id = p_order_id AND p.product_id = opt.product_id;
	DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET v_done = 1;

	OPEN v_cur1;

	REPEAT
		FETCH v_cur1 INTO v_o_p_id, v_quantity, v_stock;
		IF NOT v_done THEN
			IF v_stock &lt;= 0 THEN
				DELETE FROM order_products_tmp WHERE o_p_id = v_o_p_id LIMIT 1;
			ELSEIF v_quantity &gt; v_stock THEN
				UPDATE order_products_tmp SET o_p_quantity = v_stock WHERE o_p_id = v_o_p_id LIMIT 1;
			END IF;
		END IF;
	UNTIL v_done END REPEAT;

	CLOSE v_cur1;
END
|

</pre>

<p>
Ensuite, il suffit d'exécuter la requête SQL dans phpMyAdmin (les dernières versions supportant le délimiteur <code>DELIMITER</code>).
</p>

<p>
Pour appeler la procédure stockée, vous devez exécuter la commande <code>CALL</code>.
</p>
<pre>
CALL CHECK_STOCKS(32);
</pre>

<hr /><a href="http://res.feedsportal.com/viral/bookmark_fr.cfm?link=http%3A%2F%2Fwww.ab-d.fr%2Fdate%2F2010-03-17%2F&title=Comment+cr%C3%A9er+une+proc%C3%A9dure+stock%C3%A9e+%3F+%28+MySQL%2C+SQL+%29"><img src="http://www.ab-d.fr/images/bookmark.gif" border="0" /></a><br /><br />]]></description>
			<comments>http://www.ab-d.fr/tag/MySQL 5/</comments>
			<guid isPermaLink="false">http://www.ab-d.fr/date/2010-03-17/</guid>
		</item>

	</channel>
</rss>
