<?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: Can you use a stored procedure in a subquery?  I don&#8217;t think so. (MySQL)</title>
	<atom:link href="http://www.rustyrazorblade.com/2006/09/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rustyrazorblade.com/2006/09/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/</link>
	<description>Tech Thoughts, Mostly on LAMP - by Jon Haddad</description>
	<lastBuildDate>Mon, 23 Jan 2012 09:03:48 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Graham</title>
		<link>http://www.rustyrazorblade.com/2006/09/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/comment-page-1/#comment-71068</link>
		<dc:creator>Graham</dc:creator>
		<pubDate>Thu, 30 Jun 2011 15:28:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.rustyrazorblade.com/index.php/2006/09/27/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/#comment-71068</guid>
		<description>I&#039;m having sort of the same issue as is outlined here to be honest. I have a core stored procedure that is quite changeable functionally and as such am linking to this from several other sources as a definitive point of information.

Initially I came up with the idea of emulating the subquery fnuctionality with a comma seperated list of id&#039;s which are then parsed in a loop calling the SP every time, but this is rather slow.

For some reason it hasn&#039;t occured to me before to package this in a function as mentioned earlier by Roland, but maybe I&#039;ll look at that tomorrow.

Good post!</description>
		<content:encoded><![CDATA[<p>I&#8217;m having sort of the same issue as is outlined here to be honest. I have a core stored procedure that is quite changeable functionally and as such am linking to this from several other sources as a definitive point of information.</p>
<p>Initially I came up with the idea of emulating the subquery fnuctionality with a comma seperated list of id&#8217;s which are then parsed in a loop calling the SP every time, but this is rather slow.</p>
<p>For some reason it hasn&#8217;t occured to me before to package this in a function as mentioned earlier by Roland, but maybe I&#8217;ll look at that tomorrow.</p>
<p>Good post!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Akhil Gupta</title>
		<link>http://www.rustyrazorblade.com/2006/09/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/comment-page-1/#comment-54567</link>
		<dc:creator>Akhil Gupta</dc:creator>
		<pubDate>Sat, 06 Nov 2010 14:29:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.rustyrazorblade.com/index.php/2006/09/27/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/#comment-54567</guid>
		<description>Visual Studio and SQL Server management Studio are two different entities. So it takes a lot of time when we try an interaction with the database from Visual Studio then it takes a lot of time.

When we are using desktop applications then we do not realize this time delay, but while using web applications the time delay becomes an important factor because the database is on the remote server so a huge time delay is observed. 

Normally, the query that we write in Visual Studio is first compiled and then executed in Sql Server which then returns the results. So, this is why a time delay is observed.

The answer to this is Stored Procedure usage. The Stored Procedure is stored in compiled format in SQL server so when we call it from Visual Studio application then it is just executed. Thus saving a lot of time.

An illustration of proper usage of STORED PROCEDURE has been displayed in the following video. You may see it:

http://www.visiontechno.net/studymats/storeprocedure.html</description>
		<content:encoded><![CDATA[<p>Visual Studio and SQL Server management Studio are two different entities. So it takes a lot of time when we try an interaction with the database from Visual Studio then it takes a lot of time.</p>
<p>When we are using desktop applications then we do not realize this time delay, but while using web applications the time delay becomes an important factor because the database is on the remote server so a huge time delay is observed. </p>
<p>Normally, the query that we write in Visual Studio is first compiled and then executed in Sql Server which then returns the results. So, this is why a time delay is observed.</p>
<p>The answer to this is Stored Procedure usage. The Stored Procedure is stored in compiled format in SQL server so when we call it from Visual Studio application then it is just executed. Thus saving a lot of time.</p>
<p>An illustration of proper usage of STORED PROCEDURE has been displayed in the following video. You may see it:</p>
<p><a href="http://www.visiontechno.net/studymats/storeprocedure.html" rel="nofollow">http://www.visiontechno.net/studymats/storeprocedure.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ken</title>
		<link>http://www.rustyrazorblade.com/2006/09/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/comment-page-1/#comment-15781</link>
		<dc:creator>Ken</dc:creator>
		<pubDate>Thu, 13 Mar 2008 06:13:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.rustyrazorblade.com/index.php/2006/09/27/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/#comment-15781</guid>
		<description>Using procs in subqueries is SOP in SQL Server / Oracle and taken for granted and very useful.   

Think of procs as returning a dataset - e.g. same as a query.  So any place you would use a query (i.e. SELECT) you should be able to fit a procedure.  

For example - &quot;select x from y where g in (select gs from temp)&quot; and &quot;select x from y where g in ( call ret_gs() )&quot; are typical exmples.  

Another useful place is in joins:
select x from user u inner join ( call get_groups() ) g on u.id = g.userid.

Its also important to keep the distinction clear between functions and procs.   Functions are ment for as a term in a select - e.g. select fun1(),fun2() from X.   Functions do NOT return datasets - they return single values.   In SQL Server / Oracle you are not allowed to do things like INSERT or CREATE TABLE in functions because of the horribly performance implications of doing insert/create in a function used as a term of a select.   

So don&#039;t mind me, just killing a little time after a long day of trying to get newbies to understand how to use procs as an excellent addition to the development toolkit.</description>
		<content:encoded><![CDATA[<p>Using procs in subqueries is SOP in SQL Server / Oracle and taken for granted and very useful.   </p>
<p>Think of procs as returning a dataset &#8211; e.g. same as a query.  So any place you would use a query (i.e. SELECT) you should be able to fit a procedure.  </p>
<p>For example &#8211; &#8220;select x from y where g in (select gs from temp)&#8221; and &#8220;select x from y where g in ( call ret_gs() )&#8221; are typical exmples.  </p>
<p>Another useful place is in joins:<br />
select x from user u inner join ( call get_groups() ) g on u.id = g.userid.</p>
<p>Its also important to keep the distinction clear between functions and procs.   Functions are ment for as a term in a select &#8211; e.g. select fun1(),fun2() from X.   Functions do NOT return datasets &#8211; they return single values.   In SQL Server / Oracle you are not allowed to do things like INSERT or CREATE TABLE in functions because of the horribly performance implications of doing insert/create in a function used as a term of a select.   </p>
<p>So don&#8217;t mind me, just killing a little time after a long day of trying to get newbies to understand how to use procs as an excellent addition to the development toolkit.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jon</title>
		<link>http://www.rustyrazorblade.com/2006/09/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/comment-page-1/#comment-872</link>
		<dc:creator>jon</dc:creator>
		<pubDate>Sat, 11 Nov 2006 17:16:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.rustyrazorblade.com/index.php/2006/09/27/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/#comment-872</guid>
		<description>Tyler: Exactly what I was getting at.  I&#039;m glad that this is suppored in mssql, hopefully it&#039;ll get added to MySQL as well.</description>
		<content:encoded><![CDATA[<p>Tyler: Exactly what I was getting at.  I&#8217;m glad that this is suppored in mssql, hopefully it&#8217;ll get added to MySQL as well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tyler MacLeod</title>
		<link>http://www.rustyrazorblade.com/2006/09/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/comment-page-1/#comment-857</link>
		<dc:creator>Tyler MacLeod</dc:creator>
		<pubDate>Sat, 11 Nov 2006 09:41:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.rustyrazorblade.com/index.php/2006/09/27/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/#comment-857</guid>
		<description>What you are describing here is something like

SELECT * FROM TableCreationProc(vars)

correct? msssql 2k+ has this feature, with the UDF retuning tables. A very useful and helpful feature to be sure.

I just came across your site, and thought I give one of those real world examples. The biggest use I&#039;ve found for it is a recursive select on a tree structure. It requires a temporary table, and a loop to be able to get to an undetermined amount of levels, so in this instance a view would be unable to work.</description>
		<content:encoded><![CDATA[<p>What you are describing here is something like</p>
<p>SELECT * FROM TableCreationProc(vars)</p>
<p>correct? msssql 2k+ has this feature, with the UDF retuning tables. A very useful and helpful feature to be sure.</p>
<p>I just came across your site, and thought I give one of those real world examples. The biggest use I&#8217;ve found for it is a recursive select on a tree structure. It requires a temporary table, and a loop to be able to get to an undetermined amount of levels, so in this instance a view would be unable to work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jon</title>
		<link>http://www.rustyrazorblade.com/2006/09/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/comment-page-1/#comment-101</link>
		<dc:creator>jon</dc:creator>
		<pubDate>Thu, 28 Sep 2006 05:46:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.rustyrazorblade.com/index.php/2006/09/27/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/#comment-101</guid>
		<description>&quot;Alas…I just lost my comment - invalid security code and the back button (ff) does not work.&quot;

Hmm.. bummer.  I&#039;m using a Wordpress plugin for it, maybe I&#039;ll have it check w/ ajax instead.</description>
		<content:encoded><![CDATA[<p>&#8220;Alas…I just lost my comment &#8211; invalid security code and the back button (ff) does not work.&#8221;</p>
<p>Hmm.. bummer.  I&#8217;m using a WordPress plugin for it, maybe I&#8217;ll have it check w/ ajax instead.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roland Bouman</title>
		<link>http://www.rustyrazorblade.com/2006/09/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/comment-page-1/#comment-99</link>
		<dc:creator>Roland Bouman</dc:creator>
		<pubDate>Thu, 28 Sep 2006 02:07:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.rustyrazorblade.com/index.php/2006/09/27/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/#comment-99</guid>
		<description>Jon, 

Nice going on the view!

Tip: use a UNION ALL instead of just UNION. By definition, the two sets do not contain duplicates, and UNION ALL will probably perform better than UNION.

Alas...I just lost my comment - invalid security code and the back button (ff) does not work.

Anyway, to sum it up real short: 

&quot;Lets assume that my procedure was more complex, say if it had done several queries, used some temp tables, and a few cursors.&quot;

Yes I know it is thinkable, but it is not very likely. I hope I can argue why: you are querying for data - a list of friends. That&#039;s just what it is - data.

Procedures are there to encapsulate work - that is, sequences of different statements, typically with side-effects (update table a, check input, delete from table b, parse parameter, import data in table c, but not if..etc. etc.)  

It seems very strange to me that each time that you are just looking up some data all these changes should 

&quot;I can’t really tell if you’re disagreeing with me because you don’t see the point, or if you are trying to defend the database...&quot;

I see the point, but I disagree. I&#039;m not defending MySQL - it is more a matter of programming languages in general. Different kinds of statements do different things - that&#039;s what makes it a good language. Apparent likeness in an aspect of the behaviour of certain statements does not mean you should be able to use them interchangely. 

&quot;...and suggest a workaround as “better” while dismissing the value of this missing feature.&quot;

Views  are not a workaround - they are there to encapsulate and reuse *query* logic. Procedures are there to encapsulate and reuse *processing* logic. Views normally (ideally) don&#039;t have side-effect. It is almost always an error if a view does have side effects. 

&quot;I don’t know if Oracle or MSSQL supports this either&quot;

Oracle does not, MS SQL does. I still have to see a convincing real world example there, too.

kind regards,

Roland.</description>
		<content:encoded><![CDATA[<p>Jon, </p>
<p>Nice going on the view!</p>
<p>Tip: use a UNION ALL instead of just UNION. By definition, the two sets do not contain duplicates, and UNION ALL will probably perform better than UNION.</p>
<p>Alas&#8230;I just lost my comment &#8211; invalid security code and the back button (ff) does not work.</p>
<p>Anyway, to sum it up real short: </p>
<p>&#8220;Lets assume that my procedure was more complex, say if it had done several queries, used some temp tables, and a few cursors.&#8221;</p>
<p>Yes I know it is thinkable, but it is not very likely. I hope I can argue why: you are querying for data &#8211; a list of friends. That&#8217;s just what it is &#8211; data.</p>
<p>Procedures are there to encapsulate work &#8211; that is, sequences of different statements, typically with side-effects (update table a, check input, delete from table b, parse parameter, import data in table c, but not if..etc. etc.)  </p>
<p>It seems very strange to me that each time that you are just looking up some data all these changes should </p>
<p>&#8220;I can’t really tell if you’re disagreeing with me because you don’t see the point, or if you are trying to defend the database&#8230;&#8221;</p>
<p>I see the point, but I disagree. I&#8217;m not defending MySQL &#8211; it is more a matter of programming languages in general. Different kinds of statements do different things &#8211; that&#8217;s what makes it a good language. Apparent likeness in an aspect of the behaviour of certain statements does not mean you should be able to use them interchangely. </p>
<p>&#8220;&#8230;and suggest a workaround as “better” while dismissing the value of this missing feature.&#8221;</p>
<p>Views  are not a workaround &#8211; they are there to encapsulate and reuse *query* logic. Procedures are there to encapsulate and reuse *processing* logic. Views normally (ideally) don&#8217;t have side-effect. It is almost always an error if a view does have side effects. </p>
<p>&#8220;I don’t know if Oracle or MSSQL supports this either&#8221;</p>
<p>Oracle does not, MS SQL does. I still have to see a convincing real world example there, too.</p>
<p>kind regards,</p>
<p>Roland.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jon</title>
		<link>http://www.rustyrazorblade.com/2006/09/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/comment-page-1/#comment-97</link>
		<dc:creator>jon</dc:creator>
		<pubDate>Thu, 28 Sep 2006 01:30:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.rustyrazorblade.com/index.php/2006/09/27/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/#comment-97</guid>
		<description>Roland,

I didn&#039;t think it could be done with a view at first because of my table structure.  I just got it figured out though by unioning the table to a reversed version of itself. 

create view friendlist as
(SELECT f1.fkFriend as user1, f1.fkUser as user2 from Friend f1 WHERE f1.fkFriend &lt;&gt; f1.fkUser and confirmed = &#039;Y&#039;)
union 
(SELECT f2.fkUser as user1, f2.fkFriend as user2 from Friend f2 WHERE f2.fkFriend &lt;&gt; f2.fkUser and confirmed = &#039;Y&#039;)

Oh yeah.</description>
		<content:encoded><![CDATA[<p>Roland,</p>
<p>I didn&#8217;t think it could be done with a view at first because of my table structure.  I just got it figured out though by unioning the table to a reversed version of itself. </p>
<p>create view friendlist as<br />
(SELECT f1.fkFriend as user1, f1.fkUser as user2 from Friend f1 WHERE f1.fkFriend <> f1.fkUser and confirmed = &#8216;Y&#8217;)<br />
union<br />
(SELECT f2.fkUser as user1, f2.fkFriend as user2 from Friend f2 WHERE f2.fkFriend <> f2.fkUser and confirmed = &#8216;Y&#8217;)</p>
<p>Oh yeah.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jon</title>
		<link>http://www.rustyrazorblade.com/2006/09/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/comment-page-1/#comment-96</link>
		<dc:creator>jon</dc:creator>
		<pubDate>Thu, 28 Sep 2006 00:47:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.rustyrazorblade.com/index.php/2006/09/27/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/#comment-96</guid>
		<description>Hi again Roland,

Lets assume that my procedure was more complex, say if it had done several queries, used some temp tables, and a few cursors.  I can&#039;t really tell if you&#039;re disagreeing with me because you don&#039;t see the point, or if you are trying to defend the database and suggest a workaround as &quot;better&quot; while dismissing the value of this missing feature.

I don&#039;t know if Oracle or MSSQL supports this either, so it may be something that no one has.</description>
		<content:encoded><![CDATA[<p>Hi again Roland,</p>
<p>Lets assume that my procedure was more complex, say if it had done several queries, used some temp tables, and a few cursors.  I can&#8217;t really tell if you&#8217;re disagreeing with me because you don&#8217;t see the point, or if you are trying to defend the database and suggest a workaround as &#8220;better&#8221; while dismissing the value of this missing feature.</p>
<p>I don&#8217;t know if Oracle or MSSQL supports this either, so it may be something that no one has.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roland Bouman</title>
		<link>http://www.rustyrazorblade.com/2006/09/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/comment-page-1/#comment-94</link>
		<dc:creator>Roland Bouman</dc:creator>
		<pubDate>Wed, 27 Sep 2006 23:39:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.rustyrazorblade.com/index.php/2006/09/27/can-you-use-a-stored-procedure-in-a-subquery-i-dont-think-so-mysql/#comment-94</guid>
		<description>Hi Jon,

&quot;The reason why I wanted to do it as a stored proc is because it might be done in about 50 places throughout the site. I won’t like it if I have to modify things in more than 1 place when I change something later.&quot;

Views are actually meant to solve that.

&quot;Once again, it’s not really about the example I used, but the concept of stored procs within a subquery.&quot;

Well, I don&#039;t see the &#039;concept&#039;. It&#039;s just syntax. :)

I can see that you want and need both reusability and encapsulation, if that&#039;s the concept you mean, then I agree. But I don&#039;t see why you&#039;d want to have a procedure if you could also use a view for this. I mean, to me, it&#039;s like blaming a car that it doesn&#039;t double as a motorboat. ;)

The fact that a procedure can return a resultset does not mean that it is &#039;just like&#039; a resultset: a procedure can return no resultset, one, or even multiple resultsets; a sp can even do all of these, depending on, say, the parameters or the time of day. So, IMO, it&#039;s not comparable to a subquery or table, and therefore, I don&#039;t see why you should be able to use it as such. At least not when there is a perfectly natural alternative such as a view. 

On the other hand, maybe I am missing the point, then please explain what the view cannot do that the procedure can in this particular case.

Thinking about this some more: you can actually something like the trick with the list, but I don&#039;t recommend it. It goes like this:

create a function, not a proc, that returns the list of friends:

create function friendslist(
    uid int
) returns text
return (select group_concat(fkFriend)
from (
(
SELECT fkFriend
FROM Friend
WHERE f.fkUser = [your user with friends]
UNION ALL
SELECT fkUser
FROM Friend
WHERE f.fkFriend = [your user with friends]
) f
));

Now, you can use find_in_set:

SELECT u.username, u.firstname, u.lastname
FROM users u
WHERE find_in_set(
    u.userid
,   friendslist([user with friends])
)

If you don&#039;t think this is a solution, then this would at least implement your original solution 

&quot;and saved the comma delimited list&quot;

in the database (where it belongs - it is data centered) instead of on the webserver side.</description>
		<content:encoded><![CDATA[<p>Hi Jon,</p>
<p>&#8220;The reason why I wanted to do it as a stored proc is because it might be done in about 50 places throughout the site. I won’t like it if I have to modify things in more than 1 place when I change something later.&#8221;</p>
<p>Views are actually meant to solve that.</p>
<p>&#8220;Once again, it’s not really about the example I used, but the concept of stored procs within a subquery.&#8221;</p>
<p>Well, I don&#8217;t see the &#8216;concept&#8217;. It&#8217;s just syntax. <img src='http://www.rustyrazorblade.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I can see that you want and need both reusability and encapsulation, if that&#8217;s the concept you mean, then I agree. But I don&#8217;t see why you&#8217;d want to have a procedure if you could also use a view for this. I mean, to me, it&#8217;s like blaming a car that it doesn&#8217;t double as a motorboat. <img src='http://www.rustyrazorblade.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>The fact that a procedure can return a resultset does not mean that it is &#8216;just like&#8217; a resultset: a procedure can return no resultset, one, or even multiple resultsets; a sp can even do all of these, depending on, say, the parameters or the time of day. So, IMO, it&#8217;s not comparable to a subquery or table, and therefore, I don&#8217;t see why you should be able to use it as such. At least not when there is a perfectly natural alternative such as a view. </p>
<p>On the other hand, maybe I am missing the point, then please explain what the view cannot do that the procedure can in this particular case.</p>
<p>Thinking about this some more: you can actually something like the trick with the list, but I don&#8217;t recommend it. It goes like this:</p>
<p>create a function, not a proc, that returns the list of friends:</p>
<p>create function friendslist(<br />
    uid int<br />
) returns text<br />
return (select group_concat(fkFriend)<br />
from (<br />
(<br />
SELECT fkFriend<br />
FROM Friend<br />
WHERE f.fkUser = [your user with friends]<br />
UNION ALL<br />
SELECT fkUser<br />
FROM Friend<br />
WHERE f.fkFriend = [your user with friends]<br />
) f<br />
));</p>
<p>Now, you can use find_in_set:</p>
<p>SELECT u.username, u.firstname, u.lastname<br />
FROM users u<br />
WHERE find_in_set(<br />
    u.userid<br />
,   friendslist([user with friends])<br />
)</p>
<p>If you don&#8217;t think this is a solution, then this would at least implement your original solution </p>
<p>&#8220;and saved the comma delimited list&#8221;</p>
<p>in the database (where it belongs &#8211; it is data centered) instead of on the webserver side.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

