<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: The Lack of Flexibility of Stored Procedures in MySQL</title>
	<atom:link href="http://www.rustyrazorblade.com/2010/01/the-lack-of-flexibility-of-stored-procedures-in-mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rustyrazorblade.com/2010/01/the-lack-of-flexibility-of-stored-procedures-in-mysql/</link>
	<description>Tech Thoughts, Mostly on LAMP - by Jon Haddad</description>
	<lastBuildDate>Thu, 22 Jul 2010 09:10:54 -0700</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Lance Wheeler</title>
		<link>http://www.rustyrazorblade.com/2010/01/the-lack-of-flexibility-of-stored-procedures-in-mysql/comment-page-1/#comment-49252</link>
		<dc:creator>Lance Wheeler</dc:creator>
		<pubDate>Thu, 08 Apr 2010 16:46:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=968#comment-49252</guid>
		<description>I have found that creating a view inside a stored procedure is a very easy way to do some of this with less work in certain situations.  And it is the only way to be able to use a passed in table to get the data from. example from inside a sproc:
DROP VIEW if exists temp_view;
SET @query := CONCAT(&#039;CREATE VIEW temp_view as select * from &#039;,_tableIn,&#039; where col_6  &quot;&quot; and col_5  &quot;&quot;&quot;-&quot;&quot;&quot;&#039;);
PREPARE stmt from @query;
EXECUTE stmt;

...your queries against temp_view here.

DROP VIEW if exists temp_view;

This is easy to work with with the advantage of preforming the same query on any passed table with just the selection you want to work with.  making it fast and powerful.</description>
		<content:encoded><![CDATA[<p>I have found that creating a view inside a stored procedure is a very easy way to do some of this with less work in certain situations.  And it is the only way to be able to use a passed in table to get the data from. example from inside a sproc:<br />
DROP VIEW if exists temp_view;<br />
SET @query := CONCAT(&#8217;CREATE VIEW temp_view as select * from &#8216;,_tableIn,&#8217; where col_6  &#8220;&#8221; and col_5  &#8220;&#8221;"-&#8221;"&#8221;&#8216;);<br />
PREPARE stmt from @query;<br />
EXECUTE stmt;</p>
<p>&#8230;your queries against temp_view here.</p>
<p>DROP VIEW if exists temp_view;</p>
<p>This is easy to work with with the advantage of preforming the same query on any passed table with just the selection you want to work with.  making it fast and powerful.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott Noyes</title>
		<link>http://www.rustyrazorblade.com/2010/01/the-lack-of-flexibility-of-stored-procedures-in-mysql/comment-page-1/#comment-46564</link>
		<dc:creator>Scott Noyes</dc:creator>
		<pubDate>Fri, 08 Jan 2010 13:10:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=968#comment-46564</guid>
		<description>Example 2 - looks like you forgot to actually call the procedure</description>
		<content:encoded><![CDATA[<p>Example 2 &#8211; looks like you forgot to actually call the procedure</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jon</title>
		<link>http://www.rustyrazorblade.com/2010/01/the-lack-of-flexibility-of-stored-procedures-in-mysql/comment-page-1/#comment-46530</link>
		<dc:creator>jon</dc:creator>
		<pubDate>Thu, 07 Jan 2010 02:13:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=968#comment-46530</guid>
		<description>Justin - weird - I just tried it again and it worked... really odd.  I&#039;ll update my post accordingly.  
Thanks,
Jon</description>
		<content:encoded><![CDATA[<p>Justin &#8211; weird &#8211; I just tried it again and it worked&#8230; really odd.  I&#8217;ll update my post accordingly.<br />
Thanks,<br />
Jon</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Justin Swanhart</title>
		<link>http://www.rustyrazorblade.com/2010/01/the-lack-of-flexibility-of-stored-procedures-in-mysql/comment-page-1/#comment-46529</link>
		<dc:creator>Justin Swanhart</dc:creator>
		<pubDate>Thu, 07 Jan 2010 02:12:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=968#comment-46529</guid>
		<description>Try this:

delimiter //

-- FUNCTION   Rows
-- PURPOSE    Execute the given SQL statement and store
-- PARAMETERS 
--            v_sql:   The SELECT statement to execute
--            v_table: The table name to store the rows
--                     into.  This parameter may be NULL,
--                     in which case a random temporary table
--                     name will be generated
drop procedure if exists rows 
//

create procedure rows(IN v_sql TEXT, INOUT v_table TEXT)
begin
  -- use a session variable to count how many times the rows function has been
  -- invoked in this session.  This is used to increment the automatically
  -- generated table name to prevent collisions

  -- be careful not to overwrite any user defined variable by using
  -- an ugly prefix
  IF @__rows_CNT IS NULL THEN
    SET @__rows_CNT := 1;
  ELSE
    SET @__rows_CNT := @__rows_CNT + 1;
  END IF;

  IF v_table IS NULL THEN
    SET v_table = CONCAT(&#039;test.tbl_&#039;, CONNECTION_ID(), &#039;_&#039;, @__rows_CNT);
  END IF;

  SET @__rows_SQL = CONCAT(&#039;CREATE TEMPORARY TABLE &#039;, v_table, 
                           &#039; AS &#039;, v_sql);
  PREPARE __handle from @__rows_SQL;
  EXECUTE __handle;
  DEALLOCATE PREPARE __handle;
end
//

delimiter ;

mysql&gt; call rows(&quot;select 1 from dual&quot;, @out);Query OK, 0 rows affected (0.03 sec)mysql&gt; select @out;
+---------------------+
&#124; @out                &#124;
+---------------------+
&#124; test.tbl_32804915_3 &#124; 
+---------------------+
1 row in set (0.00 sec)

mysql&gt; select * from test.tbl_32804915_3;
+---+
&#124; 1 &#124;
+---+
&#124; 1 &#124; 
+---+
1 row in set (0.00 sec)</description>
		<content:encoded><![CDATA[<p>Try this:</p>
<p>delimiter //</p>
<p>&#8211; FUNCTION   Rows<br />
&#8211; PURPOSE    Execute the given SQL statement and store<br />
&#8211; PARAMETERS<br />
&#8211;            v_sql:   The SELECT statement to execute<br />
&#8211;            v_table: The table name to store the rows<br />
&#8211;                     into.  This parameter may be NULL,<br />
&#8211;                     in which case a random temporary table<br />
&#8211;                     name will be generated<br />
drop procedure if exists rows<br />
//</p>
<p>create procedure rows(IN v_sql TEXT, INOUT v_table TEXT)<br />
begin<br />
  &#8212; use a session variable to count how many times the rows function has been<br />
  &#8212; invoked in this session.  This is used to increment the automatically<br />
  &#8212; generated table name to prevent collisions</p>
<p>  &#8212; be careful not to overwrite any user defined variable by using<br />
  &#8212; an ugly prefix<br />
  IF @__rows_CNT IS NULL THEN<br />
    SET @__rows_CNT := 1;<br />
  ELSE<br />
    SET @__rows_CNT := @__rows_CNT + 1;<br />
  END IF;</p>
<p>  IF v_table IS NULL THEN<br />
    SET v_table = CONCAT(&#8217;test.tbl_&#8217;, CONNECTION_ID(), &#8216;_&#8217;, @__rows_CNT);<br />
  END IF;</p>
<p>  SET @__rows_SQL = CONCAT(&#8217;CREATE TEMPORARY TABLE &#8216;, v_table,<br />
                           &#8216; AS &#8216;, v_sql);<br />
  PREPARE __handle from @__rows_SQL;<br />
  EXECUTE __handle;<br />
  DEALLOCATE PREPARE __handle;<br />
end<br />
//</p>
<p>delimiter ;</p>
<p>mysql&gt; call rows(&#8221;select 1 from dual&#8221;, @out);Query OK, 0 rows affected (0.03 sec)mysql&gt; select @out;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
| @out                |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
| test.tbl_32804915_3 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
1 row in set (0.00 sec)</p>
<p>mysql&gt; select * from test.tbl_32804915_3;<br />
+&#8212;+<br />
| 1 |<br />
+&#8212;+<br />
| 1 |<br />
+&#8212;+<br />
1 row in set (0.00 sec)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Justin Swanhart</title>
		<link>http://www.rustyrazorblade.com/2010/01/the-lack-of-flexibility-of-stored-procedures-in-mysql/comment-page-1/#comment-46528</link>
		<dc:creator>Justin Swanhart</dc:creator>
		<pubDate>Thu, 07 Jan 2010 01:30:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=968#comment-46528</guid>
		<description>re:TEMPORARY TABLES

Maybe you were not in the test database or you are using a buggy version of mysql:

mysql&gt; delimiter //
mysql&gt; 
mysql&gt; create procedure test_proc3()
    -&gt; begin
    -&gt;   CREATE TEMPORARY TABLE test.my_table (id serial);
    -&gt; end
    -&gt; //
Query OK, 0 rows affected (0.03 sec)

mysql&gt; 
mysql&gt; delimiter ;
mysql&gt; 
mysql&gt; call test_proc3();
Query OK, 0 rows affected (0.09 sec)

mysql&gt; use test;
Database changed
mysql&gt; 
mysql&gt; select * from my_table;
Empty set (0.00 sec)

I use temporary tables A LOT in the Flexviews stored procedures.  I use one for a recursive stored procedure to track state at the proper depth, for instance.</description>
		<content:encoded><![CDATA[<p>re:TEMPORARY TABLES</p>
<p>Maybe you were not in the test database or you are using a buggy version of mysql:</p>
<p>mysql&gt; delimiter //<br />
mysql&gt;<br />
mysql&gt; create procedure test_proc3()<br />
    -&gt; begin<br />
    -&gt;   CREATE TEMPORARY TABLE test.my_table (id serial);<br />
    -&gt; end<br />
    -&gt; //<br />
Query OK, 0 rows affected (0.03 sec)</p>
<p>mysql&gt;<br />
mysql&gt; delimiter ;<br />
mysql&gt;<br />
mysql&gt; call test_proc3();<br />
Query OK, 0 rows affected (0.09 sec)</p>
<p>mysql&gt; use test;<br />
Database changed<br />
mysql&gt;<br />
mysql&gt; select * from my_table;<br />
Empty set (0.00 sec)</p>
<p>I use temporary tables A LOT in the Flexviews stored procedures.  I use one for a recursive stored procedure to track state at the proper depth, for instance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Antony Curtis</title>
		<link>http://www.rustyrazorblade.com/2010/01/the-lack-of-flexibility-of-stored-procedures-in-mysql/comment-page-1/#comment-46527</link>
		<dc:creator>Antony Curtis</dc:creator>
		<pubDate>Thu, 07 Jan 2010 01:16:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=968#comment-46527</guid>
		<description>Perhaps you want to examine the use of PREPARE...

SET @foo_query = CONCAT(&quot;SELECT * FROM &quot;, @tab);
PREPARE foo_stmt FROM @foo_query;
EXECUTE foo_stmt;


You can file this last tip into the category of &quot;Well, it doesn&#039;t help me much.&quot; ... In the work that I did for External Language Stored Procedures, I implemented table functions. This would allow you to do perform a join with the derived table generated from the resultset from the stored procedure. I did a presentation involving it at last year&#039;s MySQL Conference.</description>
		<content:encoded><![CDATA[<p>Perhaps you want to examine the use of PREPARE&#8230;</p>
<p>SET @foo_query = CONCAT(&#8221;SELECT * FROM &#8220;, @tab);<br />
PREPARE foo_stmt FROM @foo_query;<br />
EXECUTE foo_stmt;</p>
<p>You can file this last tip into the category of &#8220;Well, it doesn&#8217;t help me much.&#8221; &#8230; In the work that I did for External Language Stored Procedures, I implemented table functions. This would allow you to do perform a join with the derived table generated from the resultset from the stored procedure. I did a presentation involving it at last year&#8217;s MySQL Conference.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
